“This is the second day of my participation in the First Challenge 2022, for more details: First Challenge 2022”.

An overview of the

Spring AOP is one of the spring features, and in most cases, we use AOP features to enhance functional methods. To gain a thorough understanding of AOP principles, it is best to understand in advance Java’s implementation of two dynamic proxies and the Spring container initialization bean principle.

The resources

1 Spring source analysis container full life cycle graphic illustration 2Java two ways to achieve dynamic proxy

Sping uses AOP code samples

/ / 1. Open the aop
@EnableAspectJAutoProxy
@Configuration
public class MyConfig {}//2. Define the section
@Aspect
@Component
public class MyLogAspectJ {

    // Requires AOP functionality classes to customize pointcut expressions
    @Pointcut(value = "execution(* com.example.springboottestservice.aop.TestService.*(..) )"
    public void pointcut(a){}

    @Before(value = "pointcut()")
    public void logBefore(a){
        System.out.println("... logBefore...");
    }

    @After(value = "pointcut()")
    public void logAfter(a){
        System.out.println("... logAfter..."); }}Copy the code

@enableAspectJAutoProxy annotation details

Using the @ Import annotations, Import a ImportBeanDefinitionRegistrar class, will happen when performing BeanFactoryProcessor callback, the purpose is to add some components to the container

Register a AnnotationAwareAspectJAutoProxyCreator type bean, is a BeanPostProcessor, object creation after call BeanPostProcessor callback methods, return a proxy object, enhanced the implementation class

As you can see, the AOP implementation is done based on the Spring lifecycle with intervention container initialization and bean creation (the intervention part in blue)

Analysis of proxy object creation process

Son BeanPostProcessor interface postProcessBeforeInstantiation (beanClass, beanName) in the original object creation to perform before, Used to initialize the notification part (initialize all facets @aspect first) (only initialize once will be cached)

Life cycle method of BeanPostProcessor postProcessAfterInitialization will be done on the object creation, finished assembly, began to initialize the calling, this method will be ongoing bean packaging as a proxy object

As you can see, you create a proxy object, look for notifications directly, construct a MethodInterceptor, bind callbacks and notifications ahead of time, and finally create a proxy object using Cglibv, replacing the returned proxy object with the one that is actually in the container

Proxy objects perform process analysis

The way to execute a proxy object is to execute a callback, during which notifications are wrapped as MethodInterceptor. All notifications form a chain of interceptors, executed one by one

Best practices

1. Methods of the same class call each other, enhancing the effectiveness of the problem, the most obvious is transaction effectiveness

// The solution is very simple, just loop through the dependency injection itself and become the bean to be called
@Component
public class F {

    @Autowired
    private F f;

    public void a(a){
        System.out.println(Perform a "F");
    }

    public void b(a){
        System.out.println("F b");
    }

    public void ca(a){
        System.out.println("F perform ca"); f.a(); }}Copy the code

2. The implementation principle of Spring-cache is actually the use of AOP features

We still create it as a proxy object, and then just enhance the logic using a CacheInterceptor, which is used to perform some cache creation logic before or after we execute the target method

conclusion

1. By intervening in spring BeanFactoryPostProcessor need to register the bean, which is used to intervene the type of bean created AspectJAwareAdvisorAutoProxyCreator BeanPostProcessor

2. AspectJAwareAdvisorAutoProxyCreator creation process, generate a proxy object, will need to inform method and binding agent

3. Execute the method, then execute the previous callback, and aop functionality is complete