前言

在之前的文章中,我们已经对Spring源码中的一些核心概念进行了分析。由于篇幅限制,我们并没有详细解释ApplicationContext类所继承的父接口及其作用。因此,本文将单独为ApplicationContext进行详细说明,包括其继承的父接口及其作用。

ApplicationContext父接口MessageSource

大家应该都比较熟悉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”。

ResourcePatternResolver

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

以上只是简单的示例,具体使用方法还需根据实际情况进行调整。

EnvironmentCapable

获取运行时环境可以使用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方法去获取。

ApplicationEventPublisher

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 {/** * 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框架,加深对其理解和掌握。

ps:以上内容,纯属个人见解,有任何问题下方评论!关注博主公众号,源码专题、面试精选、AI最新扩展等你来看!原创编写不易,转载请说明出处!