小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。
关于Spring的源码相关功能
1 引入ApplicationContext
ApplicationContext是Spring的一个核心接口,允许容器通过应用程序上下文环境创建,获取,管理bean.
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
/**
* Return the unique id of this application context.
* @return the unique id of the context, or {@code null} if none
*/
@Nullable
String getId();
/**
* Return a name for the deployed application that this context belongs to.
* @return a name for the deployed application, or the empty String by default
*/
String getApplicationName();
/**
* Return a friendly name for this context.
* @return a display name for this context (never {@code null})
*/
String getDisplayName();
/**
* Return the timestamp when this context was first loaded.
* @return the timestamp (ms) when this context was first loaded
*/
long getStartupDate();
/**
* Return the parent context, or {@code null} if there is no parent
* and this is the root of the context hierarchy.
* @return the parent context, or {@code null} if there is no parent
*/
@Nullable
ApplicationContext getParent();
AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
}
复制代码
ApplicationContext提供的功能:
- 访问应用程序组件的Bean工厂方法. 从org.springframework.beans.factory.ListableBeanFactory继承而来.
public interface ListableBeanFactory extends BeanFactory {
......
}
复制代码
- 通用方式加载文件资源的能力.从org.springframework.core.io.support.ResourcePatternResolver继承而来.
package org.springframework.core.io.support;
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public interface ResourcePatternResolver extends ResourceLoader {
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
Resource[] getResources(String var1) throws IOException;
}
复制代码
- 向注册监听器发布事件的能力. 从org.springframework.context.ApplicationEventPublisher继承而来.
@FunctionalInterface
public interface ApplicationEventPublisher {
default void publishEvent(ApplicationEvent event) {
publishEvent((Object) event);
}
void publishEvent(Object event);
}
复制代码
- 解析消息的能力,支持国际化. 从org.springframework.context.MessageSource继承而来.
public interface MessageSource {
@Nullable
String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale);
String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException;
String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;
}
复制代码
- 从父上下文继承,后代上下文中的定义总是优先级.单个父上下文可以被整个web应用程序使用,而每个servlet都有自己独立于任何其他servlet的子上下文.
2 关于ApplicationListener的说明
1 ApplicationListener简介
ApplicationContext事件机制是属于设计模式中的观察者设计模式,通过ApplicationEvent类和ApplicationListener接口实现事件处理.
当容器中有一个ApplicationListener对象, 当ApplicationContext发布ApplicationEvent事件时,ApplicationListener对象会被自动触发, 需要由程序来控制. 此外Spring中也内置了一下事件.
内置事件 | 说明 |
---|---|
ContextRefreshedEvent | ApplicationContext 被初始化或刷新时,该事件被发布。这也可以在 ConfigurableApplicationContext接口中使用 refresh() 方法来发生。此处的初始化是指:所有的Bean被成功装载,后处理Bean被检测并激活,所有Singleton Bean 被预实例化,ApplicationContext容器已就绪可用 |
ContextStartedEvent | ConfigurableApplicationContext (ApplicationContext子接口)接口中的 start() 方法启动 ApplicationContext 时,该事件被发布。你可以调查你的数据库,或者你可以在接受到这个事件后重启任何停止的应用程序 |
ContextStoppedEvent | ConfigurableApplicationContext 接口中的 stop() 停止 ApplicationContext 时,发布这个事件。你可以在接受到这个事件后做必要的清理的工作 |
ContextClosedEvent | ConfigurableApplicationContext 接口中的 close() 方法关闭 ApplicationContext 时,该事件被发布。一个已关闭的上下文到达生命周期末端;它不能被刷新或重启 |
RequestHandledEvent | 是 web-specific 事件,告诉所有 bean HTTP 请求已经被服务处理。只能应用于使用DispatcherServlet的Web应用。在使用Spring作为前端的MVC控制器时,当Spring处理用户请求结束后,系统会自动触发该事件 |
2 ApplicationListener案列
1 准备一个SpringBoot 环境
2 创建一个自定义的监听器
@Component
public class DemoApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
System.out.println(event);
System.out.println("TestApplicationListener............................");
}
}
复制代码
根据上述可知, ContextRefreshedEvent内置事件, 是ApplicationContext 被初始化或刷新时会发布, 即监听器可以收到回调信息.
3 启动项目,查看日志
3 关于ApplicationContext的说明
1ApplicationContext的简介
从上述可知ApplicationContext具有发布事件的能力, 是从ApplicationEventPublisher接口继承来的. 而Spring中的事件使用,需要继承ApplicationEvent类或ApplicationContextEvent抽象类,抽象类中只有一个构造函数,且带有一个Object类型的参数作为事件源,且该事件源不能为null,因此我们需要在自己的构造函数中执行super(Object)。
public class EventObject implements java.io.Serializable {
private static final long serialVersionUID = 5516075349620653480L;
/**
* The object on which the Event initially occurred.
*/
protected transient Object source;
/**
* Constructs a prototypical Event.
*
* @param source The object on which the Event initially occurred.
* @exception IllegalArgumentException if source is null.
*/
public EventObject(Object source) {
if (source == null)
throw new IllegalArgumentException("null source");
this.source = source;
}
....
}
复制代码
2ApplicationContext的案列
1 准备一个SpringBoot 环境
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
testEvent();
}
// @Bean
// public FeignInterceptor feignInterceptor(){
// return new FeignInterceptor();
// }
// 测试事件
public static void testEvent() {
ApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
DemoEvent demoEvent = new DemoEvent(context, "小明", 20);
context.publishEvent(demoEvent);
}
}
复制代码
2 创建一个自定义的监听器
@Component
public class Demo2ApplicationListener implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// 针对自定义事件做处理
if (event instanceof DemoEvent) {
System.out.println(event);
DemoEvent demoEvent = (DemoEvent) event;
System.out.println("姓名:" + demoEvent.getUsername() + ",年龄: " + demoEvent.getAge());
System.out.println("自定义DemoEvent事件............................");
}
}
}
复制代码
3 创建一个自定义的事件
public class DemoEvent extends ApplicationEvent {
private String username;
private int age;
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
public DemoEvent(Object source, String username, int age) {
super(source);
this.username = username;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
复制代码