
JAVA 开发微指南
读完需要
分钟
速读仅需 4 分钟
/ 前言 /
在之前的文章中,我们已经对 Spring 源码中的一些核心概念进行了分析。由于篇幅限制,我们并没有详细解释 ApplicationContext 类所继承的父接口及其作用。因此,本文将单独为 ApplicationContext 进行详细说明,包括其继承的父接口及其作用。
/ ApplicationContext 父接口 /
1
大家应该都比较熟悉 MessageSource,它用于国际化,许多项目都会使用它。使用 MessageSource 的基本步骤如下:
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);String message = applicationContext.getMessage("test", null, new Locale("en"));System.out.println(message);
你需要在 resources 路径下创建相应的语言文件。例如,在本文的代码示例中,我们使用了“en”语言,因此需要创建 messages_en.properties 文件,其内容如下:
test=b
这样,当我们获取“test”语言时,就会得到“b”。
2
ResourcePatternResolver 主要用于获取资源,即资源加载,可以加载某个文件的内容。具体步骤如下:
// 创建一个Spring容器AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);// String message = applicationContext.getMessage("test", null, new Locale("en"));// System.out.println(message);Resource resource = applicationContext.getResource("classpath:spring.properties");System.out.println(resource.contentLength());
除此之外,ResourcePatternResolver 还有其他用法,例如:
// 创建一个Spring容器AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);// String message = applicationContext.getMessage("test", null, new Locale("en"));// System.out.println(message);Resource resource = applicationContext.getResource("https://www.baidu.com");System.out.println(resource.contentLength());System.out.println(resource.getURL());//还可以获取多个资源Resource[] resources = applicationContext.getResources("classpath:com/xiaoyu/*.class");Arrays.stream(resources).forEach(System.out::println);
以上只是简单的示例,具体使用方法还需根据实际情况进行调整。
3
获取运行时环境可以使用 ApplicationContext 的 getEnvironment 方法,具体用法如下:
// 创建一个Spring容器AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);applicationContext.getEnvironment().getPropertySources().forEach(System.out::println);System.out.println("================");applicationContext.getEnvironment().getSystemEnvironment().forEach((k, v) -> System.out.println(k + " : " + v));System.out.println("================");applicationContext.getEnvironment().getSystemProperties().forEach((k, v) -> System.out.println(k + " : " + v));System.out.println("================");System.out.println(applicationContext.getEnvironment().getProperty("sun.jnu.encoding"));System.out.println(applicationContext.getEnvironment().getProperty("xiaoyu"));
@PropertySource("classpath:spring.properties")
注意,可以使用@PropertySource 注解将 spring.properties 添加到运行时环境,然后通过 getProperty 方法去获取。
4
ApplicationEventPublisher 是一个事件发布器,我们可以通过 ApplicationContext 来发布一个相应的事件,具体步骤如下:
// 创建一个Spring容器AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);//发布自己的事件applicationContext.publishEvent(new MyEvent("xiaoyu"));
定义自己的事件:
public class MyEvent extends ApplicationEvent {private String message;public MyEvent(String message) {super(message);this.message = message;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}}
创建一个事件监听器,可以监听所有事件,也可以单独监听自己的事件。如果想要监听所有事件,直接去掉泛型即可。Spring 的事件也可以监听到,因此在监听部分需要自己判断是什么事件。具体步骤如下:
@Componentpublic class MyEventListener implements ApplicationListener<MyEvent> {/*** Handle an application event.** @param event the event to respond to*/@Overridepublic void onApplicationEvent(MyEvent event) {if (event instanceof MyEvent) {System.out.println(((MyEvent) event).getMessage());}System.out.println(event);}}
/ OrderComparator /
这里注意下,OrderComparator 并不是 ApplicationContext 的父接口,它是 Spring 内部提供的一种比较器,用于排序实现了 Order 接口或者@Order 注解的 bean。虽然在工作中我们也会用到排序,但单独写一篇文章可能并不必要,因此在这里简单提一下。
他是 Spring 内部提供的一种比较器,用于排序实现了 order 接口或者@order 注解,首先定义两个具体的 bean,具体用法如下:
public class First implements Ordered {@Overridepublic int getOrder() {return 1;}}
public class Second implements Ordered {@Overridepublic int getOrder() {return 2;}}
First first = new First();Second second = new Second();Arrays.asList(first, second).stream().sorted(OrderComparator.INSTANCE).forEach(System.out::println);
这样就会升序排序,数值越小越在前面,如果使用的是注解形式的@order,则使用下面的实例:
First first = new First();Second second = new Second();Arrays.asList(first, second).stream().sorted(AnnotationAwareOrderComparator.INSTANCE).forEach(System.out::println);
注意,OrderComparator 只适用于实现了 Ordered 接口或者@Order 注解的 bean,如果需要对其他类型的对象进行排序,可以使用其他比较器。
/ 结语 /
至此,Spring 的核心概念解析告一段落,但这只是一个开始,后续我们将深入理解 Spring 的源码。因此,建议仔细查看 Spring 的核心关键类,对于后续查看 Spring 源码会非常有帮助。同时,在实际项目中多关注 Spring 框架,加深对其理解和掌握。
本篇文章来源于微信公众号: JAVA 开发微指南
微信扫描下方的二维码阅读本文

Comments NOTHING