天天看點

spring源碼學習--注解加載bean執行個體化

<context:component-scan base-package=“com.zoe.post,com.zoe.cust”/>

多個package的話,base-package之間用,隔開

@Service @Component 注釋支援的前提是包掃描,通過掃描器掃描到有注解的類并封裝成BeanDefinition對象

spring源碼學習--注解加載bean執行個體化

包掃描的主要邏輯方法:org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse、

1、掃描路徑.class字尾的檔案

2、判斷類上面是否有注釋

3、GenericBeanDefinition genericBeanDefinition = newGenericBeanDefinition();

genericBeanDefinition.setBeanClass(BeanClass.class);

4、完成BeanDefinition注冊

spring源碼學習--注解加載bean執行個體化

約定大于配置:很多配置都預設了,不需要過多改和配置。

use-default-filters:預設掃描過濾器,預設true,預設的掃描@Service @Component ,非預設的話,可以自定義注釋.

預設掃描,會把Component加進去,沒有添加Service,為什麼會掃描到Service,原因是Service和Component有繼承關系

spring源碼學習--注解加載bean執行個體化
spring源碼學習--注解加載bean執行個體化

ConfigurationClassPostProcessor類來完成@Configuration @Bean 注解的掃描

CommonAnnotationBeanPostProcessor類來完成@PostContruct @PreDestroy @Resource 注解的掃描

spring源碼學習--注解加載bean執行個體化