Java类获取Spring的ApplicationContext
实现
1.创建一个类让其实现org.springframework.context.ApplicationContextAware接口来让Spring在启动的时候为我们注入ApplicationContext对象.
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class MyApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
//声明一个静态变量保存
public void setApplicationContext(ApplicationContext contex) throws BeansException {
this.context=contex;
}
public static ApplicationContext getContext(){
return context;
}
public final static Object getBean(String beanName){
return context.getBean(beanName);
}
public final static Object getBean(String beanName, Class<?> requiredType) {
return context.getBean(beanName, requiredType);
}
}2.在applicationContext.xml文件中配置此bean,以便让Spring启动时自动为我们注入ApplicationContext对象.
例:
<!-- 这个bean主要是为了得到ApplicationContext 所以它不需要其它属性-->
<bean class="org.ing.springutil.MyApplicationContextUtil"></bean>
3.有了这个ApplicationContext之后我们就可以调用其getBean("beanName")方法来得到由Spring 管理所有对象.
原理
Spring中提供一些Aware相关的接口,BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,其中最常用到的是ApplicationContextAware。 实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入ApplicationContext的实例。 ApplicationContextAware提供了publishEvent()方法,实现Observer(观察者)设计模式的事件传播机,提供了 针对Bean的事件传播功能。通过Application.publishEvent方法,我们可以将事件通知系统内所有的 ApplicationListener。
Spring事件处理一般过程:
◆定义Event类,继承org.springframework.context.ApplicationEvent。
◆编写发布事件类Publisher,实现org.springframework.context.ApplicationContextAware接口。
◆覆盖方法setApplicationContext(ApplicationContext applicationContext)和发布方法publish(Object obj)。
◆定义时间监听类EventListener,实现ApplicationListener接口,实现方法onApplicationEvent(ApplicationEvent event)。
MessageEvent Java代码
import org.springframework.context.ApplicationEvent;
/**
* 定义事件信息
* @author new
*
*/
public class MessageEvent extends ApplicationEvent {
private String message;
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return message;
}
public MessageEvent(Object source, String message) {
super(source);
this.message = message;
// TODO Auto-generated constructor stub
}
private static final long serialVersionUID = 1L;
}
Publisher Java代码
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Publisher implements ApplicationContextAware {
private ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
// TODO Auto-generated method stub
this.context = arg0;
}
public void publish(String message){
context.publishEvent(new MessageEvent(this,message));
}
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
Publisher pub = (Publisher) ctx.getBean("publisher");
pub.publish("Hello World!");
pub.publish("The quick brown fox jumped over the lazy dog");
}
}
MessageEventListener Java代码
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class MessageEventListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// TODO Auto-generated method stub
if(event instanceof MessageEvent){
MessageEvent msEvent = (MessageEvent)event;
System.out.println("Received: " + msEvent.getMessage());
}
}
}
在运行期,ApplicationContext会自动在当前的所有Bean中寻找ApplicationListener接口的实现,并将其作为事件接 收对象。当Application.publishEvent方法调用时,所有的ApplicationListener接口实现都会被激发,每个 ApplicationListener可根据事件的类型判断是否是自己需要处理的事件,如上面的ActionListener只处理 ActionEvent事件。