初识AnnotationConfigApplicationContext(1)

前言

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.

  1. internalConfigurationAnnotationProcessor
    类型为org.springframework.context.annotation.ConfigurationClassPostProcessor,该类实现了BeanFactoryPostProcessor,用来处理class上标注了注解org.springframework.context.annotation.Configuration

  2. internalEventListenerProcessor
    类型为org.springframework.context.event.EventListenerMethodProcessor,该类实现了BeanFactoryPostProcessor,用来处理标注了注解org.springframework.context.event.EventListener的method

  3. 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");
  1. 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
  1. 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值