Annotation-based Spring source code analysis
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class); // contains configuration classes and packet scanning
Copy the code
1. Component addition
@ComponentScan: Specifies the package to be scanned
@bean: Registers a Bean in the container; Type is the type of the return value. Id defaults to the method name as id
@ScopePrototype: the ioc container starts without calling methods to create objects in the container. The method is called to create the object each time it is fetched; Singleton: Singleton (default) : The IOC container start calls methods to create objects to be placed in the IOC container. Request: create an instance on the same request session: Create an instance on the same session@LazyLazy loading: single-instance beans: objects are created by default when the container is started; Lazy loading: The container starts without creating objects. The first time an object is created and initialized using a (get)Bean;Copy the code
Bean life cycle (bean creation -- initialization ---- destruction process)1), specify initialization and destruction methods; through@BeanSpecify init-method and destroy-method;2), make the Bean implement InitializingBean (define initialization logic), DisposableBean (define destruction logic);3), can use JSR250;@PostConstruct: After the bean is created and the property assignment is complete; To perform the initialization method@PreDestroy: Notify us of the cleanup before the container destroys the bean4), BeanPostProcessor 【interface】 :beanAfterprocessor; inbeanDo some processing before and after initialization;postProcessBeforeInitialization: Works before initializationpostProcessAfterInitialization: Works after initializationBeanPostProcessorThe principle ofpopulateBean(beanName.mbd.instanceWrapper); To bean bean initializeBean instantiated attribute assignment {applyBeanPostProcessorsBeforeInitialization (wrappedBean, beanName);// Get all the beanpostProcessors in the container; Each execution beforeInitialization, but returns null, jump out of the for loop, don't perform the back of the BeanPostProcessor postProcessorsBeforeInitializationinvokeInitMethods(beanName, wrappedBean, mbd); Perform custom initialization applyBeanPostProcessorsAfterInitialization (wrappedBean, beanName); } Spring's underlying use of BeanPostProcessor; Bean assignment, inject other components,@Autowired, lifecycle annotation function,@AsyncSuch functions are implemented by XXX BeanPostProcessor;Copy the code
@configuration: Configuration class == Configuration file, telling Spring that this is a Configuration class
@Component, @service,@Controller,@Repository: useDefaultFilters = true (enabled by default) will scan for its own classes
@Conditional: Uniform setting of components in a class. All bean registrations configured in this class will take effect if the current condition is met;
@Conditional({WindowsCondition.class}) // The beans in the widow system @configuration only take effect
Copy the code
@Primary
@ Import: Import components, id is the full name of the class components, by default or implement ImportSelector or ImportBeanDefinitionRegistrar full name of the class
@Import({Color.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
Copy the code
Factory pattern: Implement FactoryBean For example,FactoryBean
// The factory Bean gets the object created by calling getObject
Object bean2 = applicationContext.getBean("colorFactoryBean");
// To get the factory Bean itself, we need to prefix the ID with &:&colorFactoryBean
Object bean4 = applicationContext.getBean("&colorFactoryBean");
Copy the code
Conclusion:
Register components in the container.1), package scan + component annotation (@Controller/@Service/@Repository/@Component) [Self-written class]2),@Bean[Components in imported third-party packages]3),@Import[Quickly import a component into a container]1),@import (components to be imported into containers); The component is automatically registered in the container, and the id defaults to the full class name2), ImportSelector: Returns an array of all-class names of the components to be imported;3), ImportBeanDefinitionRegistrar: manually register beans into the container4), using FactoryBeans provided by Spring;1), the default is to get the object created by the factory bean's call to getObject2To get the factory Bean itself, we need to add && colorFactoryBean before the IDCopy the code
Component assignment
@Value
@Autowired
@PropertySource
@PropertySources
@Profile
Component injection
The method parameters
Constructor injection
ApplicationContextAware
xxxAware
Four, AOP
@EnableAspectJAutoProxy
@Before/@After/@AfterReturning/@AfterThrowing/@Around
@Pointcut
Declarative transactions
@EnableTransactionManagement
@Transactional