自定义pointcut,spring自供一个ComposablePointcutpointcut
他有5个构造方法,现在不过滤类的,只过滤方法的通知
java 代码
- ComposablePointcut()
- Create a default ComposablePointcut, with ClassFilter.TRUE and MethodMatcher.TRUE.
- ComposablePointcut(ClassFilter classFilter)
- Create a ComposablePointcut for the given ClassFilter, with MethodMatcher.TRUE.
- ComposablePointcut(ClassFilter classFilter, MethodMatcher methodMatcher)
- Create a ComposablePointcut for the given ClassFilter and MethodMatcher.
- ComposablePointcut(MethodMatcher methodMatcher)
- Create a ComposablePointcut for the given MethodMatcher, with ClassFilter.TRUE.
- ComposablePointcut(Pointcut pointcut)
- Create a ComposablePointcut based on the given Pointcut.
要实现在的通知
java 代码
- package composablepointcut;
-
- import java.lang.reflect.Method;
-
- import org.springframework.aop.MethodBeforeAdvice;
-
- public class BeforeAdvice implements MethodBeforeAdvice {
-
-
- public void before(Method method, Object[] args, Object target)
- throws Throwable {
- System.out.println("Before method: " + method);
- }
-
- }
目标对象
java 代码
- package composablepointcut;
-
- public class Person {
- public String getName(){
- return "java";
- }
- public void setAge(){
- System.out.println("setAge");
- }
- public String getAddress(){
- return "china";
- }
-
- }
实现staticmethodmatcher接口,实现对setAge() 方法的通知
java 代码
- package composablepointcut;
-
- import java.lang.reflect.Method;
-
- import org.springframework.aop.support.StaticMethodMatcher;
-
- class SetAgeMethodMatcher extends StaticMethodMatcher {
- public boolean matches(Method method, Class cls) {
- return "setAge".equals(method.getName());
- }
- }
java 代码实现对set开头的 方法的通知
- package composablepointcut;
-
- import java.lang.reflect.Method;
-
- import org.springframework.aop.support.StaticMethodMatcher;
-
- class SetterMethodMatcher extends StaticMethodMatcher {
-
- public boolean matches(Method method, Class cls) {
- return (method.getName().startsWith("set"));
- }
-
- }
java 代码实现对getname() 方法的通知
- package composablepointcut;
-
- import java.lang.reflect.Method;
-
- import org.springframework.aop.support.StaticMethodMatcher;
-
-
- class GetNameMethodMatcher extends StaticMethodMatcher {
-
- public boolean matches(Method method, Class cls) {
- return (method.getName().startsWith("get"));
- }
-
- }
创建一个advisor ,使用DefaultPointcutAdvisor
java 代码
- Advisor advisor = new DefaultPointcutAdvisor(pc,
- new SimpleBeforeAdvice());
java 代码
- package composablepointcut;
-
- import org.springframework.aop.Advisor;
- import org.springframework.aop.ClassFilter;
- import org.springframework.aop.framework.ProxyFactory;
- import org.springframework.aop.support.ComposablePointcut;
- import org.springframework.aop.support.DefaultPointcutAdvisor;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import aop1.SimpleBeforeAdvice;
-
- public class ComposablePointcutTest {
- private static Person getProxy(ComposablePointcut pc, Person target) {
- Advisor advisor = new DefaultPointcutAdvisor(pc,
- new SimpleBeforeAdvice());
- // 没用proxyFactoryBean 配制xml麻烦的要死,直接使用proxyfactory
- ProxyFactory pf = new ProxyFactory();
- pf.setTarget(target);
- pf.addAdvisor(advisor);
-
- return (Person) pf.getProxy();
- }
- private static void runProxy( Person proxy) {
- proxy.getAddress();
- proxy.getName();
- proxy.setAge();
- }
- public static void main(String[] args) {
- Person target = new Person();
- //这里只实现一个过滤setAge方法,可以通compostable.union()组合过滤方法,
- //intersection方法取交集
- ComposablePointcut compostable = new ComposablePointcut(ClassFilter.TRUE,new SetAgeMethodMatcher());
- Person proxy = ComposablePointcutTest.getProxy(compostable, target);
- ComposablePointcutTest.runProxy(proxy);
- }
- }
spring还提供一个nameMatchMethodPointcut,使用对方法名的过滤.
void setMappedName(String mappedName)
Convenience method when we have only a single method name to match.
void setMappedNames(String[] mappedNames)
Set the method names defining methods to match.
可以通过这两个方法实现过滤的通知
还有RegexpMethodPointcut,支持正则表达式过滤通知方法