Entry point

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44778952/article/details/89468505

Pointcut Interface

pointcut is the entry point, is a collection of many connection points, the purpose is to find the right place to apply the notification. In Spring currently supports only one form of connection points, that is, non-public static method call, you can use the notification.
For the starting point, there are two aspects need to identify. A is applied to the classes in the end, the second method is applicable to those above. From this idea, Pointcut just the interface provides two methods

  • ClassFilter getClassFilter()

    • ClassFilter
      • Designed to check whether the target class is a suitable entry point
      • 方法 boolean matches(Class<?> clazz)
      • 常量 ClassFilter TRUE=TrueClassFilter.INSTANCE
  • MthodMather getMthodMathcer()

    • MthodMather
      • Examples of suitable mainly for application to check whether the method section
      • Method boolean isRuntime ()
      • 方法 boolean matches(Method m,Class<?> targetClass)
      • 方法 boolean matches(Method m,Class<?> targetClass,Object[] args)
      • MethodMatcher implement interface supports both static and dynamic types, depending on the method called before the method call matches the return value is determined isRuntime
        • If it returns true: (<?> Method m, Class targetClass) dynamically, a first call matches method, if returns true, the subsequent calls matches (Method m, Class targetClass, Object [] args <?>)
          • The first method returns to true matches, matches the latter method will be called once per method call matching, a method of matching the parameters
        • Return false: static, only one call matches method (Method m, Class targetClass <?>)

Spring in dealing Pointcut match, the first call is to check whether the target class method getClassFilter match, if returns true, then call getMthodMather inspection method matches

API level

Here Insert Picture Description
The figure above, all classes with Pointcut associated with the interface, which has an abstract StaticMthodMatcherPointcut class (static), there is an abstract class DynamicMethodMatcherPointcut (dynamic).

StaticMethodMatcherPointcut

Sample Code
Interface

package com.csdn.springaop.target;

public interface MyInterface {
	public void method1();
	public void method2() throws Exception ;
}

Target class

package com.csdn.springaop.target;

public class Target implements MyInterface {
	public void method1() {
		System.out.println("Target method1");
	}
	public void method2() throws Exception {
		throw new Exception("Target method2 Error!");
	}
}

Test code

package com.csdn.springaop.test;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import com.csdn.springaop.advice.MyAfterReturningAdvice;
import com.csdn.springaop.advice.MyAroundAdvice;
import com.csdn.springaop.advice.MyBeforeAdvice;
import com.csdn.springaop.advice.MyThrowsAdvice;
import com.csdn.springaop.target.MyInterface;
import com.csdn.springaop.target.Target;

public class SpringTest2 {
	@Test
	public  void test1() {
		Target target=new Target();
		MyBeforeAdvice myBeforeAdvice=new MyBeforeAdvice();
	
		ProxyFactory proxyFactory =new ProxyFactory();
		proxyFactory.setTarget(target);
		proxyFactory.addAdvice(myBeforeAdvice);

		MyInterface target1=(MyInterface)proxyFactory.getProxy();
		target1.method1();
		System.out.println("开始运行toString方法");
		target1.toString();
	}
}

The above operating results

MyBeforeAdvice
Target method1
开始运行toString方法
MyBeforeAdvice

We can see from the results, toString method is called proxy target class also performed Before notification, because we call addAdvice method ProxyFactory plant, and there is no limit those methods can be applied to the notification.
Below we will modify the code under test

@Test
	public  void test2() {
		Target target=new Target();
		MyBeforeAdvice myBeforeAdvice=new MyBeforeAdvice();
	
		ProxyFactory proxyFactory =new ProxyFactory();
		proxyFactory.setTarget(target);
		
		Pointcut pointcut=new StaticMethodMatcherPointcut() {

			@Override
			public boolean matches(Method method, Class<?> targetClass) {
				return "method1".equals(method.getName());
			}
			
		};
		DefaultPointcutAdvisor advisor=new DefaultPointcutAdvisor(pointcut,myBeforeAdvice);
	 
		proxyFactory.addAdvisor(advisor);
		
		MyInterface target1=(MyInterface)proxyFactory.getProxy();
	 
		target1.method1();
		System.out.println("开始运行toString方法");
		target1.toString();
		 
	}

After the original test code, by adding a notice to the agent factory, and there is no way to inform the application of limited, so we added a specific pointcut that can be defined in the end what kind of methods which can refer to the notice. According to the core concepts of AOP, which have been combined into a section, we can already use the following interfaces

  • Advisor Interface
    • A cut face, there are two sub-interfaces
      • PointcutAdvisor
        • Based on the entry point
        • DefaultPointcutAdvisor is the default implementation of PointcutAdvisor
      • IntroductionAdvisor
        • With the introduction of relevant

Output

MyBeforeAdvice
Target method1
开始运行toString方法

It can be seen only method1 method is applied to the notification.

DynamicMethodMatcherPointcut

The above is applied to a static MethodMatcher abstract class, the following then apply a dynamic MthodMatcher abstract class, and override method which matches with three parameters

@Test
	public  void test3() {
		Target target=new Target();
		MyBeforeAdvice myBeforeAdvice=new MyBeforeAdvice();
	
		ProxyFactory proxyFactory =new ProxyFactory();
		proxyFactory.setTarget(target);
		
		Pointcut pointcut=new DynamicMethodMatcherPointcut() {
			
			@Override
			public boolean matches(Method method, Class<?> targetClass, Object... args) {
				return "method1".equals(method.getName())&&args.length==1&&args[0].equals("one");
			}
		};
		DefaultPointcutAdvisor advisor=new DefaultPointcutAdvisor(pointcut,myBeforeAdvice);
	 
		proxyFactory.addAdvisor(advisor);
		
		MyInterface target1=(MyInterface)proxyFactory.getProxy();
	 
		System.out.println("运行method()");
		target1.method1();
		System.out.println("运行method(\"one\")");
		target1.method1("one");
		System.out.println("运行method(\"two\")");
		target1.method1("two");
		 
	}

Override a method in the target method1

public void method1(String s) {
		System.out.println("Target method1"+s);
}

The above operating results

运行method()
Target method1
运行method("one")
MyBeforeAdvice
Target method1one
运行method("two")
Target method1two

As can be seen, only method1 method, and when circumstances call parameters are "one" is applied to the notification.

DefaultPointcutAdvisor

Construction method

  • public DefaultPointcutAdvisor()
  • public DefaultPointcutAdvisor(Advice advice)
  • public DefaultPointcutAdvisor(Pointcut pointcut,Advice advice)

Common method

  • setPointcut(Pointcut pointcut)
  • setAdvice(Advice advice)
  • setOrder(int order)

Guess you like

Origin blog.csdn.net/weixin_44778952/article/details/89468505