它是 Applicationcontext 的父接口
它才是Spring的核心容器,主要的Applicationcontext实现都组合了它的功能
表面上只有getBean
实际上控制反转、基本的依赖注入、直至 Bean的生命周期的各种功能,都由它的实现类提供
//设置多语言显示
ConfigurableApplicationContext context = SpringApplication.run(ManagerServerApplication.class, args);
system. out.println(context.getNessage( code: "hi",args: null,Locale.CHINA));
system.out.println(context.getNessage( code: "hi",args: null,Locale.ENGLISH));
system.out.println(context.getNessage( code:"hi",args: null,Locale.JAPANESE));
//加载资源
Resource[] resources = context.getResources( “classpath*:HETA-INF/spring .factories")
for (Resource resource : resources) {
system.out.println(resource);
}
//获取配置文件的值
system.out.println(context.getEnvironment().getProperty("java_home"));
system.out.println(context.getEnvironment().getProperty( "server.port"));
//发布事件
//参数为自定义的用户注册事件
@Autowired
private ApplicationEventPublisher context;
public void register() {
log.debug("用户注册");
context.publishEvent(new UserRegisteredEvent(this));
)
//在需要接收事件的方法上添加 @EventListener 注解
@EventListener
public void aaa(UserRegisteredEvent event) {
log.debug("{(J", event);
//注册发邮件发短信等行为
}
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
//bean的定义 calss scope 初始化 销毁
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(自定义beanName.class).setScope("singleton").getBeanDefinition();
beanFactory.registerBeanDefinition(自定义的beanName,beanDefinition);
//给beanFactory 添加一些常用的后处理器
AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
//给beanFactory 后处理器的主要功能 , 补充一些bean定义
beanFactory.getBeansOfType(BeanFactoryPostProcessor.class).values().forEach(beanFactoryPostProcessor -> {
beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
});
//bean后处理器,针对bean的生命周期的各个阶段提供扩展, 例如 @Autowired @Resource ...
beanFactory.getBeansOfType(BeanFactoryPostProcessor.class).values().forEach(beanFactory::addBeanPostProcessor);
for (String name : beanFactory.getBeanDefinitionNames()) {
System.out.println(name);
}
beanFactory不会做的事
// 1. 较为经典的容嚣,基于 classpath下 xml格式的配置文件来创建
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( " classpath下 xml文件名" );
// 2. 基于磁盘路经下xml格式的配置文件来创建
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext( " 磁盘路径下 xml文件名 " );
//3. 较为经典的容器 ,基于java 配置类来创建
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext( " 自定义配置类.class" );
// 4. 较为经典的容器, 基于java配置类来创建. 用于web环境
AnnotationConfigServletWebServerApplicationContext context3 = new AnnotationConfigServletWebServerApplicationContext( " 自定义配置类.class" );
//如果使用第四种,那么自定义的配置类需要包含如下三个bean
@Bean
public ServletWebServerFactory servletWebServerFactory(){
return new TomcatServletWebServerFactory();
}
@Bean
public DispatcherServlet dispatcherServlet(){
return new DispatcherServlet();
}
@Bean
public DispatcherServletRegistrationBean dispatcherServletRegistrationBean(){
return new DispatcherServletRegistrationBean(dispatcherServlet() , "/");
}
//打印beanname
for (String name : context.getBeanDefinitionNames()) {
System.out.println(name);
}