ApplicationContext具备的能力
路径解析
ConfigurableApplicationContext applicationContext = SpringApplication.run(ApplicationContextTest01.class, args);
/**
* ResourcePatternResolver
* classpath*: 可以加载jar包里的文件
*/
Resource[] resources = applicationContext.getResources("classpath:application.yaml");
//Resource[] resources = applicationContext.getResources("classpath*:META-INF/spring.factories");
for (Resource resource : resources) {
System.err.println(resource);
}
读取环境配置
String property = applicationContext.getEnvironment().getProperty("server.port");
System.err.println(property);
国际化
// String welcome = applicationContext.getMessage("welcome", new String[]{"zhoujunlin", DateUtil.now()}, Locale.CHINA);
String welcome = applicationContext.getMessage("welcome", new String[]{"zhoujunlin", DateUtil.now()}, Locale.US);
System.err.println(welcome);
事件发布
事件、事件发布者与事件监听者
public class RegisterEvent extends ApplicationEvent {
public RegisterEvent(Object source) {
super(source);
}
}
@Component
public class EventPublisher {
@Resource
private ApplicationEventPublisher applicationEventPublisher;
public void publishEvent() {
applicationEventPublisher.publishEvent(new RegisterEvent(this));
}
}
@Component
public class EventConsumer {
@EventListener
public void handler(RegisterEvent registerEvent) {
System.out.println("RegisterEvent by EventConsumer source:" + registerEvent.getSource());
}
}
/**
* ApplicationEventPublisher
*/
//applicationContext.publishEvent(new RegisterEvent(applicationContext));
applicationContext.getBean(EventPublisher.class).publishEvent();
BeanFactory与ApplicationContext
在没有ApplicationContext时,是怎么样将BeanDefinition加载到BeanFactory中的? (ApplicationContext是对BeanFactory进行更进一步封装增强)
定义Bean
static class Bean01 {
}
@Data
static class Bean02 {
private Bean01 bean01;
}
定义bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bean01" class="com.you.meet.nice.test.web.spring.applicationcontext.ApplicationContextTest02.Bean01"/>
<bean id="bean02" class="com.you.meet.nice.test.web.spring.applicationcontext.ApplicationContextTest02.Bean02">
<property name="bean01" ref="bean01"/>
</bean>
<!--
相当于 AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory); 此功能
加载一些内置后处理器
-->
<!-- <context:annotation-config/> -->
</beans>
加载xml中的BeanDefinition到BeanFactory
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
System.out.println("加载xml之前。。。");
for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
System.out.println(beanDefinitionName);
}
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource("bean01.xml"));
System.out.println("加载xml之后。。。");
for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
System.out.println(beanDefinitionName);
}
System.out.println(beanFactory.getBean(Bean02.class).getBean01());
常见的几种ApplicationContext
ClassPathXmlApplicationContext
// ClassPathXmlApplicationContext加载BeanDefinition到Bean容器
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean01.xml");
for (String beanDefinitionName : classPathXmlApplicationContext.getBeanDefinitionNames()) {
System.out.println(beanDefinitionName);
}
System.out.println(classPathXmlApplicationContext.getBean(Bean02.class).getBean01());
FileSystemXmlApplicationContext
FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("/src/test/resources/bean01.xml");
for (String beanDefinitionName : fileSystemXmlApplicationContext.getBeanDefinitionNames()) {
System.out.println(beanDefinitionName);
}
System.out.println(fileSystemXmlApplicationContext.getBean(Bean02.class).getBean01());
AnnotationConfigApplicationContext
@Configuration
static class Config {
@Bean
public Bean01 bean01() {
return new Bean01();
}
@Bean
public Bean02 bean02(Bean01 bean01) {
Bean02 bean02 = new Bean02();
bean02.setBean01(bean01);
return bean02;
}
}
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Config.class);
/**
* 多加载了内置处理器
* internalConfigurationAnnotationProcessor、internalAutowiredAnnotationProcessor、internalCommonAnnotationProcessor
* internalEventListenerProcessor、internalEventListenerFactory
* 相当于多了此功能
* AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
*/
for (String beanDefinitionName : annotationConfigApplicationContext.getBeanDefinitionNames()) {
System.out.println(beanDefinitionName);
}
System.out.println(annotationConfigApplicationContext.getBean(Bean02.class).getBean01());
AnnotationConfigServletWebServerApplicationContext
@Configuration
static class WebConfig {
@Bean
public ServletWebServerFactory tomcatServletWebServerFactory() {
return new TomcatServletWebServerFactory();
}
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Bean
public DispatcherServletRegistrationBean dispatcherServletRegistrationBean(DispatcherServlet dispatcherServlet) {
return new DispatcherServletRegistrationBean(dispatcherServlet, "/");
}
@Bean("/hello")
public Controller hello() {
return (request, response) -> {
response.getWriter().print("hello world");
return null;
};
}
}
运行web服务
AnnotationConfigServletWebServerApplicationContext webServerApplicationContext =
new AnnotationConfigServletWebServerApplicationContext(WebConfig.class);