1、配置元信息
基于XML文件(<beans><bean>标签)
基于注解的配置(spring2.5之后)
基于java的配置(spring3.0之后,@Configuration,@Bean,@Import,@DependsOn)
2、实例化容器
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
3、使用容器
User user= context.getBean("user", User.class);
user.process();
或
GenericApplicationContext context = new GenericApplicationContext();
new XmlBeanDefinitionReader(context)
.loadBeanDefinitions("services.xml", "daos.xml");
context.refresh();
4、BeanDefinition
5、命名bean
6、实例化bean
构造函数实例化
静态工厂方法实例化
实例工厂方法实例化
7、依赖注入
基于构造器
介于setter方法
depends-on
延迟初始化
自动装配
方法注入
8、bean scope
Singleton
Prototype
具有原型bean依赖关系的单例bean
Request, Session, Application, WebSocket
9、自定义bean的性质
初始化回调--@PostConstruct、InitializingBean#afterPropertiesSet()、 @Bean#initMethod
销毁回调--@PreDestroy、DisposableBean#destroy()、 @Bean#destroyMethod
启动和关闭回调--Lifecycle、LifecycleProcessor
在非web应用程序中优雅地关闭Spring IoC容器--registerShutdownHook()
Aware接口
10、容器的扩展点
BeanPostProcessor:实现自定义bean
BeanFactoryPostProcessor:自定义配置元信息
FactoryBean:自定义实例化逻辑
11、基于注解的容器配置
隐式注册的后处理器:
AutowiredAnnotationBeanPostProcessor,
CommonAnnotationBeanPostProcessor,
PersistenceAnnotationBeanPostProcessor,
RequiredAnnotationBeanPostProcessor
@Autowired
@Primary:按类型
@Qualifier:按名字
CustomAutowireConfigurer
@Resource
@Value:注入外部化属性
@PostConstruct and @PreDestroy
12、类路径扫描和管理组件
@Component及其派生
组合注解
自动检测类并注册Bean定义:@ComponentScan
使用过滤器自定义扫描:includeFilters or excludeFilters
13、JSR-330注解
@Inject----@Autowired
@Named---@Component
@Named---@Component
14、基于java的容器配置
@Bean and @Configuration
使用AnnotationConfigApplicationContext实例化Spring容器
@Import
15、环境
@Profile
激活:ctx.getEnvironment().setActiveProfiles("development");
@PropertySource
16、LoadTimeWeaver
17、ApplicationContext的附加功能
通过MessageSource接口访问i18n风格的消息
通过ResourceLoader接口访问资源,例如url和文件
事件发布,即通过使用ApplicationEventPublisher接口发布到实现ApplicationListener接口的bean
通过HierarchicalBeanFactory接口加载多个(分层的)上下文,让每个上下文都关注于一个特定的层
18、BeanFactory
DefaultListableBeanFactory
GenericApplicationContext