一、spring事件
spring的事件有如下两个成员。
1、ApplicationEvent,容器事件,由容器发布
2、ApplicationListener 监听器,可以由容器中的任何监听器Bean担任
(1)先顶一个spring的容器事件:
package cn.study.basic; import org.springframework.context.ApplicationEvent; public class EmailEvent extends ApplicationEvent { private String address; private String text; public EmailEvent(Object source) { super(source); } public EmailEvent(Object source, String address, String text) { super(source); this.address = address; this.text = text; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
(2)编写容器监听器代码:
package cn.study.basic; import org.springframework.context.ApplicationListener; public class EmailListener implements ApplicationListener<EmailEvent> { @Override public void onApplicationEvent(EmailEvent arg0) { System.out.println(arg0 instanceof EmailEvent); if (arg0 instanceof EmailEvent) { EmailEvent ee = (EmailEvent) arg0; System.out.println("address:" + ee.getAddress()); } else { System.out.println("container:" + arg0); } } }
(3)、bean.xml文件中加入如下配置:
<bean class="cn.study.basic.EmailListener"></bean>
(4)、测试方法
package cn.study.basic.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.study.basic.EmailEvent; public class TestAMain { @Test public void testApp() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); EmailEvent emailEvent = new EmailEvent("object", "address", "test"); context.publishEvent(emailEvent); } }
运行代码,执行结果如下所示:
true
address:address
二、bean获取spring容器
在web开发的过程中,spring容器通常使用声明式方法配置产生,开发者只需要在web.xml中配置相应的Listener,在启动的时候就会初始化Spring容器,但是某些比较特殊的时候,容器中的Bean需要主动访问Spring容器,有如下两种方式。
(1)、实现BeanFactoryAware接口,在实现接口的同时,必须实现如下方法。
public void setBeanFactory(BeanFactory arg0) throws BeansException { }
(2)、实现ApplicationContextAware接口,同时需要实现如下方法
@Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { }
下面使用第二种方法来实现以下小示例,首先实现接口,代码如下:
package cn.study.basic; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class GetApContext implements ApplicationContextAware { private ApplicationContext ctx; @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { this.ctx = arg0; } public ApplicationContext getContext() { return ctx; } }
还需要在bean.xml文件中配置bean,如下
<bean class="cn.study.basic.GetApContext"></bean>
测试方法如下:
package cn.study.basic.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.study.basic.GetApContext; public class TestContext { @Test public void testContext() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); GetApContext ctx = (GetApContext) context.getBean("spContext"); System.out.println(ctx.getContext()); System.out.println(context==ctx.getContext()); } }
测试结果如下所示:
--->org.springframework.context.support.ClassPathXmlApplicationContext@2c11b4c2: startup date [Fri Oct 03 14:04:27 CST 2014]; root of context hierarchy
true
原文:http://www.cnblogs.com/gyouxu/p/4004936.html