This article can be reproduced at will, please retain the author when reprintedZevi @ the nuggetsAttribution and provenanceJuejin. Cn/post / 684490…

preface

Spring Aop is fundamentally implemented by Java dynamic proxies and Cglib, but we have a few problems:

  • Which technology does Spring Aop use by default to implement Aop?
  • When are Java dynamic proxies used? When is CGLIb used?
Well, let's start looking for answers to these two questions in the Spring source code.

start

To prepare

First let’s write a simple Spring Aop environment

Dependent packages:

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> < version > 5.0.8. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Aspectj < / groupId > < artifactId > aspectjweaver < / artifactId > < version > 1.9.0 < / version > < / dependency >Copy the code

Spring configuration class AppConfig:

@Configuration
@ComponentScan(basePackages = "com.dahafo")
@EnableAspectJAutoProxy
public class AppConfig {

}
Copy the code

Aop processing classes:

@aspect @Component public class AppAspect {// Define a Pointcut @pointcut ("execution(* com.dahafo.dao.. *. * (..) )")
    public void pointCut(){

    }

    @After("pointCut()")
    public void after(){
        System.out.println("aspect---after---"); }}Copy the code

Dao layer interface

public interface Dao {

    public void save();
}
Copy the code

Of course you need a dao layer method, HelloDao:

@Component
public class HelloDao implements Dao{

    public void save() {
        System.out.println("dao---save---"); }}Copy the code

Okay, now that we’re done with the code, let’s do another main method to launch,

public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(AppConfig.class); context.start(); Dao dao=context.getBean(Dao.class); dao.save(); }}Copy the code

Let’s take a look. The console printed successfully

dao---save---
aspect---after---
Copy the code

That we write aop can run successfully

Start debugging

On Dao Dao = Context.getBean (dao.class), Debug starts and enters the getBean(dao.class) method, Go AbstractApplicationContext# getBean (Java. Lang. Class < T >) method return getBeanFactory () getBean (requiredType), enter the code execution. Return getBeanFactory().getBean(requiredType). To enter DefaultListableBeanFactory# getBean (Java. Lang. Class < T >, Java. Lang. Object…). Methods. NamedBeanHolder

namedBean = resolveNamedBean(requiredType, args) Return new NamedBeanHolder<>(beanName, requiredType, args)) Go to AbstractBeanFactory#doGetBean, go to Object sharedInstance = getSingleton(beanName), enter, enter again. The DefaultSingletonBeanRegistry# getSingleton (Java. Lang. String, Boolean) method, We look at the first line of the Object singletonObject = this. SingletonObjects. Get (beanName) enclosing singletonObjects, is a ConcurrentHashMap, There are many classes in it, and we can find the class with the key as helloDao. If we go down a step, we can see that the singletonObject has a value

How to predict the future, please listen to the next decomposition, deep digging Spring Aop bottom implementation (two) to continue

This article can be reproduced at will, please retain the author when reprintedZevi @ the nuggetsAttribution and provenanceJuejin. Cn/post / 684490…