Spring is a hierarchical full-stack lightweight open source framework, based on IoC and AOP as the kernel, provides a large number of enterprise application technologies such as presentation layer SpringMVC and business layer transaction management, but also can integrate many well-known third-party frameworks and class libraries in the open source world. It has become the most used Open source framework for Java EE enterprise applications.

 

 

Contents of this article:

  1. Summary of the Spring
  2. core idea
  3. Handwritten implementation of IoC and AOP
  4. Spring IOC application
  5. Spring IOC source in depth analysis
  6. Spring AOP applications
  7. Spring AOP source depth analysis

This article mainly introduces Spring IOC, and Spring AOP source in-depth analysis, all the content of the article is from a senior architect hand compiled documents, need this document friends can pay attention to the end of the public account to obtain.

 

Spring IOC source code in-depth analysis:

  • Benefits: Improved training of code architecture thinking, in-depth understanding of the framework
  • The principle of
  1. Principle of focus: Focus on the main line
  2. Macro principles: Focus on the source code structure and business processes from God’s perspective (downplay the details of writing specific lines of code)
  • Read source code methods and skills
  1. Breakpoints (look at the call stack)
  2. Lead Lead (Find Left)
  3. Experience (doXXX in the Spring framework, where specific processing is done)
  • Spring source code construction
  1. Download source (Github)
  2. Install Gradle 5.6.3 (like Maven) Idea 2019.1 Jdk 11.0.5
  3. Import (takes some time)
  4. Compile project (order: core-OXm-context-beans-aspects – AOP)

(Project – > Tasks – >compileTestJava)

Section 1: The Spring IoC container initializes the body process

1.1 Spring IoC’s container architecture

IoC container is the core module of Spring, which abstracts object management and dependency management framework solution. Spring provides a number of containers, of which the BeanFactory is the top-level (root) container and cannot be instantiated. It defines a set of principles that all IoC containers must follow. Specific container implementations can add additional functionality, such as the common ApplicationContext. Its more specific implementation such as ClassPathXmlApplicationContext contains content, parse the XML and a series of AnnotationConfifigApplicationContext is includes annotations analytic and so on a series of content. The Spring IoC container inheritance system is smart enough to use whichever layer you need, rather than the full-featured one.

The BeanFactory top-level interface method stack is as follows

BeanFactory container inheritance system

 

From its interface design, we can see that the ApplicationContext we always use inherits the BeanFactory sub-interface, ResourceLoader, MessageSource, etc., so it provides richer functionality. Below we ClasspathXmlApplicationContext, for example, deep source of IoC container initialization process.

1.2 Key timing points in the Bean life cycle

Create a class LagouBean, let it implement several special interfaces, and respectively in the constructor of interface implementation, interface method breakpoint, observe the thread call stack, analyze the Bean object creation and management key trigger time.

LagouBean class package com. Lagou; import org.springframework.beans.BeansException; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor; importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.stereotype.Component; /** * @author * @create 2019/12/3 11:46 */ public class implements InitializingBean{/** * constructor */ public LagouBean(){system.out.println ("LagouBean constructor..." ); }/** * InitializingBean interface implementation */ public void afterPropertiesSet() throws Exception {system.out.println ("LagouBean afterPropertiesSet..." ); }}Copy the code

 

Spring AOP source depth analysis:

Note, note, due to the length of the article, all knowledge points are not here to sum up one by one, need this document friends pay attention to the end of the public number to obtain this document.

Section 1 Proxy object creation

1.1 Preparation of AOP basic use cases

Bean definition

@Component public class LagouBean { public void tech(){ System.out.println("java learning......" ); }}Copy the code

The Aspect to define

package com.lagou; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect public class LagouAspect { @Pointcut("execution(* com.lagou.*.*(..) )") public void pointcut(){ } @Before("pointcut()") public void before() { System.out.println("before method ......" ); }}Copy the code

The test case

/** * test case: */ @test public void testAopProxyBuild(){ApplicationContext ApplicationContext = newAnnotationConfigApplicationContext(SpringConfig.class); LagouBean lagouBean = applicationContext.getBean(LagouBean.class); lagouBean.tech(); }Copy the code

1.2 Timing analysis

 

We find that the LagouBean object was generated before the getBean (that is, done in the first line of initialization code) and that the object is a proxy object (Cglib proxy object). We conclude that the target Ban has completed the proxy and returned the proxy object during container initialization.

Conclusion:

Spring source code has always been a big factory often asked a point, so a good grasp of spring knowledge is a very important part of entering the big factory, the above document to show you is an architect to organize, need to pay attention to the following public account for access. Attached is a copy of the latest interview selection.