前言
spring3.0开始支持注解来启动spring容器,摒弃了以前xml繁琐的配置方式。
学习的版本为5.3.20
开始
非web环境下,使用AnnotationConfigApplicationContext启动。
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(RootConfiguration.class);
applicationContext.refresh();
查看日志输出
[logback] 236 [main] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@35851384
[logback] 248 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
[logback] 319 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
[logback] 320 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
[logback] 321 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
[logback] 322 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor
从日志里面可以看到,AnnotationConfigApplicationContext在启动时默认注册了这5个Bean.
-
internalConfigurationAnnotationProcessor
类型为org.springframework.context.annotation.ConfigurationClassPostProcessor,该类实现了BeanFactoryPostProcessor,用来处理class上标注了注解org.springframework.context.annotation.Configuration -
internalEventListenerProcessor
类型为org.springframework.context.event.EventListenerMethodProcessor,该类实现了BeanFactoryPostProcessor,用来处理标注了注解org.springframework.context.event.EventListener的method -
internalEventListenerFactory
类型为org.springframework.context.event.DefaultEventListenerFactory,该类实现了org.springframework.context.event.EventListenerFactory,从源码中可以看出该类是把标注了注解org.springframework.context.event.EventListener的method适配成org.springframework.context.event.ApplicationListenerMethodAdapter,和bean internalEventListenerProcessor搭配使用,EventListenerMethodProcessor部分源码如下
ConfigurableApplicationContext context = this.applicationContext;
Assert.state(context != null, "No ApplicationContext set");
List<EventListenerFactory> factories = this.eventListenerFactories;
Assert.state(factories != null, "EventListenerFactory List not initialized");
- internalAutowiredAnnotationProcessor
类型为org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,该类实现了BeanPostProcessor,用来处理的注解有
- org.springframework.beans.factory.annotation.Autowired
- org.springframework.beans.factory.annotation.Value
- javax.inject.Inject的注解,需要引入javax.inject-1.jar
- internalCommonAnnotationProcessor
类型为org.springframework.context.CommonAnnotationBeanPostProcessor,该继承了org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor,同时实现了BeanPostProcessor,能够处理的注解有
- javax.annotation.PostConstruct
- javax.annotation.PreDestroy
- javax.annotation.Resource
- javax.xml.ws.WebServiceRef
- javax.ejb.EJB