SpringAOP entry point in the advanced use

Previous SpringAOP use the entry point to create notification

Advanced Use SpringAOP cut point

First, using a control flow pointcuts (ControlFlowPointcut)

What is the entry point to control the flow of it? See the following code (for convenience, it is written into a common class)

class Cat {
    public void talk() {
        System.out.println("I am a cat");
    }

    public void play() {
        System.out.println("I am palying");
    }
}

class BlackCat {
    public void sleep(Cat cat) {
        cat.play();
        System.out.println("I am a blackCat , I am sleeping");
    }

}

/**
 * 创建前置通知类
 */
class BeforeAdvice implements MethodBeforeAdvice{

    @Override
    public void before(Method method, Object[] objects, @Nullable Object o) throws Throwable {
        System.out.println("这个方法被通知了"+method);
    }
}
  • Requirements: We give Cat a play () method of notification, but it does not mean that all notice at any time, call the play () method, notice when you call the play as long as blackCat the sleep () method () method, In other words:
public static void main(String[] args) {
        Cat cat = new Cat();
        cat.play();//这个调用不会被通知
        
        BlackCat blackCat = new BlackCat();
        blackCat.sleep(cat);//这个方法中调用的paly方法才会被通知
    }

Create a ControlFlowPointcut entry point

    public static void main(String[] args) {
//        Cat cat = new Cat();
//        cat.play();//这个调用不会被通知
//
//        BlackCat blackCat = new BlackCat();
//        blackCat.sleep(cat);//这个方法中调用的paly方法才会被通知


        Cat target = new Cat();

        //第一个参数是当前就是的执行要被通知的方法的类,第二个就是的执行要被通知的方法的方法名
        Pointcut pc = new ControlFlowPointcut(BlackCat.class, "sleep");
        Advisor advisor = new DefaultPointcutAdvisor(pc, new BeforeAdvice());

        ProxyFactory proxy = new ProxyFactory();
        proxy.setTarget(target);
        proxy.addAdvisor(advisor);

        Cat proxyCat = (Cat) proxy.getProxy();
        proxyCat.play();//这个方法不会被通知

        System.out.println("----------------");
        
        BlackCat blackCat = new BlackCat();
        blackCat.sleep(proxyCat);//这个方法中调用的paly方法才会被通知
    }

title

Second, the use of a combination of entry points (the ComposablePointcut)

The so-called entry point is the use of a combination of logic (or and and) to be combined entry points, such as the previous article said that several entry points , using logic written on it together. But the starting point and not directly to the combination, but the combination of entry points ClassFilter和MethodMatcher( Why is this? In this article, look at the source code Pointcut class, you understand )

  • Usage:
    the ComposablePointcut the union () means "or"
    the ComposablePointcut the intersection () means "and"

  • First class defines three MethodMatcher

/**
 * 匹配sleep方法名
 */
class SleepMethodMatcher extends StaticMethodMatcher{

    @Override
    public boolean matches(Method method, Class<?> aClass) {
        return method.getName().equals("sleep");
    }
}

/**
 * 匹配s开头
 */
class SStartMethodMatcher extends StaticMethodMatcher{

    @Override
    public boolean matches(Method method, Class<?> aClass) {
        return method.getName().startsWith("s");
    }
}

/**
 * 匹配k结尾
 */
class KEndMethodMatcher extends StaticMethodMatcher{

    @Override
    public boolean matches(Method method, Class<?> aClass) {
        return method.getName().endsWith("k");
    }
}
  • Creating an entry point
/**
 * 创建前置通知类
 */
class BeforeAdviceDemo implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] objects, @Nullable Object o) throws Throwable {
        System.out.println("这个方法被通知了" + method);
    }
}
  • Test category
public static void main(String[] args) {
        Cat target = new Cat();

        //这个构造方法要传入的是一个classFilter和methodMatcher实例
        ComposablePointcut pc = new ComposablePointcut(ClassFilter.TRUE, new KEndMethodMatcher());

//        pc.union(new SStartMethodMatcher());//匹配s开头的方法,和上边的切点是或的关系
//        pc.intersection(new SleepMethodMatcher()); //匹配sleep方法,和上边切点是和的关系

        Advisor advisor = new DefaultPointcutAdvisor(pc,new BeforeAdviceDemo());
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(target);
        proxyFactory.addAdvisor(advisor);

        Cat cat = (Cat) proxyFactory.getProxy();
        cat.talk();
    }

This ClassFilter.TRUE top and bottom of the code is actually the same, meaning that the return of classFilter is true, that is, matching all classes

new ClassFilter() {
            @Override
            public boolean matches(Class<?> aClass) {
                return true;
            }
        }

The project code address, I feel pretty good writing give a star

Guess you like

Origin www.cnblogs.com/Lyn4ever/p/12020057.html