Spring高级-容器接口-ApplicationContext的功能
ApplicationContext的实现类拥有不同的能力
- EnvironmentCapable:整合 Environment 环境(能通过它获取各种来源的配置信息)
- MessageSource: 国际化,提供一些针对不同语言翻译的能力
- ResourcePatternResolver:通配符方式获取一组 Resource 资源
- ApplicationEventPublisher:事件发布与监听,实现组件之间的解耦,类似于消息中间件
1 MessageSource(国际化)
放入资源
文件内容分别是
messages.properties 空
messages_en.properties hi=Hello
messages_ja.properties hi=こんにちは
messages_zh.properties hi=你好
代码
package com.mangoubiubiu.show; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.MessageSource; import org.springframework.core.env.EnvironmentCapable; import org.springframework.core.io.support.ResourcePatternResolver; import java.lang.reflect.Field; import java.util.Locale; import java.util.Map; @Slf4j @SpringBootApplication public class ShowApplication { public static void main(String[] args) throws Exception { //返回的结果是Spring 容器 ConfigurableApplicationContext context = SpringApplication.run(ShowApplication.class, args); System.out.println(context.getMessage("hi",null, Locale.CHINA)); System.out.println(context.getMessage("hi",null, Locale.JAPANESE)); System.out.println(context.getMessage("hi",null, Locale.ENGLISH)); } }
效果
上面ConfigurableApplicationContext 是 ApplicationContext的实现类 一样也可以用MessageSource
2 ResourcePatternResolver(通配符方式获取一组 Resource 资源)
package com.mangoubiubiu.show; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.MessageSource; import org.springframework.core.env.EnvironmentCapable; import org.springframework.core.io.Resource; import org.springframework.core.io.support.ResourcePatternResolver; import java.lang.reflect.Field; import java.util.Locale; import java.util.Map; @Slf4j @SpringBootApplication public class ShowApplication { public static void main(String[] args) throws Exception { //返回的结果是Spring 容器 ConfigurableApplicationContext context = SpringApplication.run(ShowApplication.class, args); //Resource 是 Spring 中对文件的一个抽象 Resource[] resources_yml = context.getResources("classpath:application.yml"); for (Resource resource: resources_yml) { System.out.println(resource); } //classpath:META-INF/spring.factories 只是在类路径下找 在jar包里是找不到的 //classpath*:META-INF/spring.factories 在jar里找要加* Resource[] resources = context.getResources("classpath*:META-INF/spring.factories"); for (Resource resource: resources) { System.out.println(resource); } } }
3 EnvironmentCapable 整合 Environment 环境(能通过它获取各种来源的配置信息)
package com.mangoubiubiu.show; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.MessageSource; import org.springframework.core.env.EnvironmentCapable; import org.springframework.core.io.Resource; import org.springframework.core.io.support.ResourcePatternResolver; import java.lang.reflect.Field; import java.util.Locale; import java.util.Map; @Slf4j @SpringBootApplication public class ShowApplication { public static void main(String[] args) throws Exception { //返回的结果是Spring 容器 ConfigurableApplicationContext context = SpringApplication.run(ShowApplication.class, args); //不区分大小写 //系统环境变量 System.out.println(context.getEnvironment().getProperty("java_home")); //配置信息 System.out.println(context.getEnvironment().getProperty("server.port")); //启动参数 System.out.println(context.getEnvironment().getProperty("env")); } }
启动参数
4 ApplicationEventPublisher(事件发布与监听,实现组件之间的解耦,类似于消息中间件)
简单代码
package com.mangoubiubiu.show; import org.springframework.context.ApplicationEvent; public class UserRegisteredEvent extends ApplicationEvent { public UserRegisteredEvent(Object source) { super(source); } }
package com.mangoubiubiu.show; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.MessageSource; import org.springframework.core.env.EnvironmentCapable; import org.springframework.core.io.Resource; import org.springframework.core.io.support.ResourcePatternResolver; import java.lang.reflect.Field; import java.util.Locale; import java.util.Map; @Slf4j @SpringBootApplication public class ShowApplication { public static void main(String[] args) throws Exception { //返回的结果是Spring 容器 ConfigurableApplicationContext context = SpringApplication.run(ShowApplication.class, args); //里面context 是事件源 context.publishEvent(new UserRegisteredEvent(context)); } }
package com.mangoubiubiu.show; import lombok.extern.slf4j.Slf4j; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Slf4j @Component public class Component1 { @EventListener public void aaa(UserRegisteredEvent event){ log.info("这是Component1 监听到了事件{}",event); } }
package com.mangoubiubiu.show; import lombok.extern.slf4j.Slf4j; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Slf4j @Component public class Compent2 { @EventListener public void aaa(UserRegisteredEvent event){ log.info("这是Component2 监听到了事件{}",event); } }
结合业务用户注册后发短信
事件发布类
package com.mangoubiubiu.show; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Slf4j @Component public class Component1 { @Autowired private ApplicationEventPublisher context; public void register(){ log.info("用户注册"); //注册后发布事件 context.publishEvent(new UserRegisteredEvent(this)); } }
监听类:监听类可以写多个 ,且可以写不同的业务场景
package com.mangoubiubiu.show; import lombok.extern.slf4j.Slf4j; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Slf4j @Component public class Compent2 { @EventListener public void aaa(UserRegisteredEvent event){ log.info("这是Component2 监听到了事件{}",event); log.info("发短信"); } }
调用类
package com.mangoubiubiu.show; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.MessageSource; import org.springframework.core.env.EnvironmentCapable; import org.springframework.core.io.Resource; import org.springframework.core.io.support.ResourcePatternResolver; import java.lang.reflect.Field; import java.util.Locale; import java.util.Map; @Slf4j @SpringBootApplication public class ShowApplication { public static void main(String[] args) throws Exception { //返回的结果是Spring 容器 ConfigurableApplicationContext context = SpringApplication.run(ShowApplication.class, args); context.getBean(Component1.class).register(); //获取bean 实例调用方法 } }