#Spring 配置分享
##Bean 和 容器
AbstractBeanDefinition,存储在 Spring 容器中的对象其实为包含 Metadata 的代理对象
private volatile Object beanClass;
private String scope = SCOPE_DEFAULT;
private boolean abstractFlag = false;
private boolean lazyInit = false;
private int autowireMode = AUTOWIRE_NO;
private int dependencyCheck = DEPENDENCY_CHECK_NONE;
private String[] dependsOn;
private boolean autowireCandidate = true;
private boolean primary = false;
...
所以可以按照生命周期对 Bean 进行初始化
注解版多用 @PostConstruct 和 @PreDestroy
使用 XML 可以清晰区分配置和 原始对象,SpringBoot 之后,两者之间界限变得模糊
###容器架构 真正的存储 Bean 的抽象和实现是 BeanFactory 体系,包含了对 Metadata 、 Bean 生命周期的处理,当然,也包括它自身的生命周期
Application Context,额外包含了资源加载(ResourceLoader),环境配置(EnvironmentCapable),事件体系( ApplicationEventPublisher)和国际化(MessageSource)
##Spring Boot 比 Spring FrameWork 到底多了啥
约定大于配置 —— 开发者使用注解代替 XML 配置 Bean(@Component, @Controller, @Service, @EnableConfigureProperties, @Repository, @Resource, ...)同时第三方包提供者在 META-INF 中的 spring.factories 文件中注明要自动引入的配置文件,Spring 扫描到 @EnableAutoConfiguration (或 @SpringBootApplication) 会从应用包(如 com.lls.it.BizApplication包)和第三方包中拉取所有配置 metadata ,加载到容器中
实际上还是利用 Spring 容器的生命周期做这个事情,如果纯使用注解写代码,注解 Bean 通常依赖两个类进行处理,即 ConfigurationClassPostProcessor 和 AutoConfigurationImportSelector,分别实现了 BeanFactoryPostProcessor 和 ImportSelector 接口,前者递归扫描应用目录加载配置,完成后调用后者扫描 ClassPath
主要依赖 Spring-boot 包和 Spring-boot-autoconfigure 包完成
boot 包 spring.factories