Spring 内建 Bean

Spring Ioc 容器在启动时会默认创建一些 Bean,用来后续各种处理:

  • Environment:外部化配置以及 Profiles
  • systemEnvironment Property 对象:Java 系统属性
  • systemEnvironment Map 对象:操作系统环境变量
  • MessageSource:国际化资源相关
  • LifecycleProcessor:Lifecycle Bean 处理器
  • ApplicationEventMulticaster:Spring 事件广播器
  • ConfigurationClassPostProcessor:处理 Spring 配置类
  • AutowiredAnnotationBeanPostProcessor:处理 @Autowired、@Value 注解
  • CommonAnnotationBeanPostProcessor:处理 JSR-250 注解, 如 @PostConstruct 等
  • EventListenerMethodProcessor:处理标注 @EventListener 的 Spring 事件监听方法
  • DefaultEventListenerFactory:EventListenerFactory 对象,把 @EventListener 事件监听方法适配为 ApplicationListener

 

代码示例:

测试代码

/**
 * 测试内建 Bean
 * @author ConstXiong
 */
@Configuration
public class Test {
	
	public static void main(String[] args) {
		/**
		 * 注解驱动的 context
		 */
		AnnotationConfigApplicationContext annotatedContext = new AnnotationConfigApplicationContext();
		annotatedContext.refresh();

		//外部化配置以及 Profiles
		Environment environment = annotatedContext.getBean("environment", Environment.class);
		System.out.println(environment);

		//Java 系统属性
		Properties systemProperties = annotatedContext.getBean("systemEnvironment", Properties.class);
		System.out.println(systemProperties);

		//操作系统环境变量
		Map systemEnvironment = annotatedContext.getBean("systemEnvironment", Map.class);
		System.out.println(systemEnvironment);

		//国际化
		MessageSource messageSource = annotatedContext.getBean("messageSource", MessageSource.class);
		System.out.println(messageSource);

		//Lifecycle Bean 处理器
		LifecycleProcessor lifecycleProcessor = annotatedContext.getBean("lifecycleProcessor", LifecycleProcessor.class);
		System.out.println(lifecycleProcessor);

		//Spring 事件广播器
		ApplicationEventMulticaster applicationEventMulticaster = annotatedContext.getBean("applicationEventMulticaster", ApplicationEventMulticaster.class);
		System.out.println(applicationEventMulticaster);
		
		//处理 Spring 配置类
		ConfigurationClassPostProcessor configurationClassPostProcessor = annotatedContext.getBean(ConfigurationClassPostProcessor.class);
		System.out.println(configurationClassPostProcessor);
		
		//处理 @Autowired、@Value 注解
		AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor = annotatedContext.getBean(AutowiredAnnotationBeanPostProcessor.class);
		System.out.println(autowiredAnnotationBeanPostProcessor);
		
		//处理 JSR-250 注解, 如 @PostConstruct 等
		CommonAnnotationBeanPostProcessor commonAnnotationBeanPostProcessor = annotatedContext.getBean(CommonAnnotationBeanPostProcessor.class);
		System.out.println(commonAnnotationBeanPostProcessor);
		
		//处理标注 @EventListener 的 Spring 事件监听方法
		EventListenerMethodProcessor eventListenerMethodProcessor = annotatedContext.getBean(EventListenerMethodProcessor.class);
		System.out.println(eventListenerMethodProcessor);
		
		//EventListenerFactory 对象,把 @EventListener 事件监听方法适配为 ApplicationListener
		DefaultEventListenerFactory defaultEventListenerFactory = annotatedContext.getBean(DefaultEventListenerFactory.class);
		System.out.println(defaultEventListenerFactory);
		
		//JPA 注解
//		PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor = annotatedContext.getBean(PersistenceAnnotationBeanPostProcessor.class);
//		System.out.println(persistenceAnnotationBeanPostProcessor);
	}
}

 

完整代码:015-spring-inner-bean

 


【Java学习资源】整理推荐

 

 


【Java面试题与答案】整理推荐

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值