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);
}
}
【Java学习资源】整理推荐
- 自定义 Bean 作用域
- Bean 的作用域
- maven 集成 tomcat 以 jar 的方式打包 web 应用
- Spring 依赖注入与依赖查找来源的区别
- Spring 依赖注入的处理过程与 DependencyDescriptor 的说明
- Spring 各种 Aware 接口回调注入
- Spring Bean 生命周期内的 Exception 复现
- Spring 内建 Bean
- Spring Bean 的别名
- Spring Bean 未指定名称的命名规则
- Bean 何时被 GC
- Spring Bean 延迟加载
- ObjectFactory 与 BeanFactory 的区别
- Bean 销毁的方式与顺序
- Bean 初始化的方式与顺序
- Bean 的实例化方式
- Bean的注册方式
- 什么是 BeanDefinition?
- Spring IoC 容器启动时做了什么?
- BeanFactory 与 FactoryBean 的区别
- BeanFactory 与 ApplicationContext 的区别
- Spring IoC 依赖注入的实现方式
- Spring IoC 依赖注入(支持哪些数据类型?)
- Spring bean 依赖查找
- Spring-IoC
- Spring 的核心特性