您的位置:首页 > 移动开发

码农小汪-spring框架学习之10-补充说明 ApplicationContext 的能力

2016-04-12 15:35 489 查看

补充说明 ApplicationContext 的能力

org.springframework.beans.factory 包提供了管理和操作bean 的基本功能,包含一种编程式的方式。 org.springframework.context 包增加了 ApplicationContext 接口,继承于 BeanFactory 接口,这也是为了继承其他的

接口,并且提供应用框架风格。很多人使用 ApplicationContext 完全是声明式,甚至不用编程式去创建,依靠像 ContextLoader 这样的支持类自动实例化ApplicationContext,并把此作为 Java EE web 应用的启动步骤。为了增强 BeanFactory 功能, context 包也支持下列功能:

Access to messages in i18n-style, through the MessageSource interface.

Access to resources, such as URLs and files, through the ResourceLoader interface.

Event publication to beans implementing the ApplicationListener interface, through the use of the ApplicationEventPublisher interface.

Loading of multiple (hierarchical) contexts, allowing each to be focused on one particular layer, such as the web layer of an application, through the HierarchicalBeanFactory interface.(通过 HierarchicalBeanFactory 接口,加载多级 contexts,允许关注某一层级 context,比如应用的 web 层。)

Standard and Custom Events 标准事件和自定义事件

Event handling in the ApplicationContext is provided through the ApplicationEvent class and ApplicationListener interface. If a bean that implements the ApplicationListener interface is deployed(部署) into the context, every time an ApplicationEvent gets published to the ApplicationContext(事件通知), that bean is notified. Essentially, this is the standard Observer design pattern.(观察者设计模式) Spring provides the following standard events:

EventMeans
ContextRefreshedEvent当ApplicationContext 初始化或者刷新时发布,比如,使用ConfigurableApplicationContext 接口的 refresh()方法。这里”初始化”的意思是指,所有的 bean 已经被加载、 post-processor 后处理 bean 已经被探测到并激活,单例 bean 已经 pre-instantiated 预先初始化,并且 ApplicationContext 对象已经可用。只要 context 上下文未关闭,可以多次触发刷新动作, 某些ApplicationContext 支持”热”刷新。比如,XmlWebApplicationContext 支持热刷新, GenericApplicationContext 就不支持。
ContextStartedEvent当ApplicationContext 启动时候发布,使用 ConfiruableApplicationContext 接口的start()方法。这里的“启动”意思是指,所有的 Lifecycle 生命周期 bean 接收到了明确的启动信号。通常,这个信号用来在明确的“停止”指令之后重启beans,不过也可能是使用了启动组件,该组件并未配置自动启动,比如:组件在初始化的时候并未启动。
ContextStoppedEvent当 ApplicationContext 停止时发布,使用 ConfigurableApplicationContext 接口的 stop()方法。这里的“停止”的意思是指所有的 Lifecycle 生命周期 bean 接收到了明确的停止信号。一个停止了的 context 上下文可以通过start()调用来重启。
ContextClosedEvent当ApplicationContext 关闭时候发布,使用 ConfigurableApplicationContext 接口的 close()方法。这里“关闭”的意思是所有的单例 bean 已经销毁。一个关闭的context 上下文达到的生命周期的重点。不能刷新,不能重启
RequestHandledEvent是一个 web 专用事件,告诉所有的 beans:一个 HTTPrequest 正在处理。这个时间在 reqeust 处理完成之后发布。该事件仅适用于使用 Spring 的 DispatcherServlet 的 web 应用。

You can also create and publish your own custom events. This example demonstrates a simple class that extends Spring’s ApplicationEvent base class:

自定义事件进行处理哦~

源码:ApplicationEvent

public abstract class ApplicationEvent extends EventObject {

/** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = 7099057708183571937L;

/** System time when the event happened */
private final long timestamp;

/**
* Create a new ApplicationEvent.
* @param source the component that published the event (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}

/**
* Return the system time in milliseconds when the event happened.
*/
public final long getTimestamp() {
return this.timestamp;
}

}


源码:ApplicationEventPublisher

/**
* Interface that encapsulates event publication functionality.
* Serves as super-interface for ApplicationContext.
*
* @author Juergen Hoeller
* @since 1.1.1
* @see ApplicationContext
* @see ApplicationEventPublisherAware
* @see org.springframework.context.ApplicationEvent
* @see org.springframework.context.event.EventPublicationInterceptor
*/
public interface ApplicationEventPublisher {

/**
* Notify all listeners registered with this application of an application
* event. Events may be framework events (such as RequestHandledEvent)
* or application-specific events.
* @param event the event to publish
* @see org.springframework.web.context.support.RequestHandledEvent
*/
void publishEvent(ApplicationEvent event);

}


源码:

/**
* Interface to be implemented by any object that wishes to be notified
* of the ApplicationEventPublisher (typically the ApplicationContext)
* that it runs in.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 1.1.1
* @see ApplicationContextAware
*/
public interface ApplicationEventPublisherAware extends Aware {

/**
* Set the ApplicationEventPublisher that this object runs in.
* <p>Invoked after population of normal bean properties but before an init
* callback like InitializingBean's afterPropertiesSet or a custom init-method.
* Invoked before ApplicationContextAware's setApplicationContext.
* @param applicationEventPublisher event publisher to be used by this object
*/
void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);

}


源码:Aware 看到没有,这个就是面向接口的编程,并不停供默认的实现。

public interface Aware {

}


看看我们自己的实现吧!

public class BlackListEvent extends ApplicationEvent {

private final String address;
private final String test;

public BlackListEvent(Object source, String address, String test) {
super(source);
this.address = address;
this.test = test;
}

// accessor and other methods...

}


这个就是我们的观察者模式啦,我们要发布一个事件,当有变化的时候通知我,我的去注册啊~

为了发布自定义 ApplicationEvent,得调用 ApplicationEventPublisher 接口上的publishEvent()方法。通常是使用一个已经注册为 Spring bean 的ApplicationEventPublisherAware 接口的实现类来完成的。

public class EmailService implements ApplicationEventPublisherAware {

private List<String> blackList;
private ApplicationEventPublisher publisher;

public void setBlackList(List<String> blackList) {
this.blackList = blackList;
}

public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}

public void sendEmail(String address, String text) {
if (blackList.contains(address)) {
BlackListEvent event = new BlackListEvent(this, address, text);
publisher.publishEvent(event);
return;
}
// send email...
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: