背景
学习spring源码所做笔记
Bean的创建生命周期
bean初始化流程图
扫描bean
AppConfig 定义一个扫描类
用@ComponentScan(“com.test”) 注解扫描路径
扫描包中@Component,@RestController,@Configuration和@Service注释的类
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
设置bean的配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml")
@Bean
用@Bean去注入bean对象
代码块
静态代码块
代码块
构造器
默认使用无参构造器
如果没有无参构造器,并且有多个构造器,可以用@Autowired去定义使用的构造器
初始化
调用@PostConstruct注解的方法
实现initializingBean中afterPropertiesSet 方法
实现BeanPostProcessor中postProcessBeforeInitialization
初始化后
实现BeanPostProcessor中postProcessAfterInitialization
代理对象
AOP利用BeanPostProcessor接口,生成代理对象,去代理对应类。