6. IOC小结
-
AnnotationConfigApplicationContext:
-
配置类
-
包扫描
-
-
组件添加:
-
@ComponentScan
-
@Bean
-
指定初始化销毁
-
初始化其他方式
-
(1)InitializingBean(初始化设置值之后)
-
(2)InitializingBean(初始化设置值之后)
-
(3)JSR250:@PostConstruct、@PreDestroy
-
-
-
BeanPostProcessor
-
@Configuration
-
@Component
-
@Service
-
@Controller
-
@Repository
-
@Conditional
-
@Primary
-
@Lazy
-
@Scope
-
@Import
-
ImportSelector
-
工厂模式
-
FactoryBean:&beanName获取Factory本身
-
-
-
组件赋值
-
@Value
-
@Autowired
-
(1)@Qualifier
-
(2)@Resources(JSR250)
-
(3)@Inject(JSR330,需要导入javax.inject)
-
-
-
@PropertySource
-
@PropertySources
-
@Profile
-
(1)Environment
-
(2)-Dspring.profiles.active=test
-
-
组件注入
-
方法参数
-
构造器注入
-
xxxAware
-
ApplicationContextAware
-
ApplicationContextAwareProcessor
-
if (bean instanceof Aware) {
if (bean instanceof EnvironmentAware) {
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
}
if (bean instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
}
if (bean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
}
if (bean instanceof ApplicationEventPublisherAware) {
((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
}
if (bean instanceof MessageSourceAware) {
((MessageSourceAware) bean).setMessageSource(this.applicationContext);
}
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}