02-ApplicationContext

ApplicationContext具备的能力

image.png

路径解析

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);

国际化

image.png
image.png

// 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);
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

层巅余落日

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值