|
| 1 | +#分析 |
| 2 | + |
| 3 | +Spring的事件(Application Event)为Bean与Bean之间的消息通信提供了支持。 |
| 4 | +当一个Bean处理完一个任务之后,希望另一个Bean知道并能做出相应的处理,这时我们就需要让另外一个Bean监听当前Bean所发送的事件。 |
| 5 | + |
| 6 | +Spring的事件需要遵循如下流程: |
| 7 | +1、自定义事件,集成ApplicationEvent。 |
| 8 | +2、定义事件监听器,实现ApplicationListener |
| 9 | +3、使用容器发布容器 |
| 10 | + |
| 11 | +进行本示例的演示,需要先配置好Maven和Spring哦、 |
| 12 | +见: |
| 13 | +<a href="http://blog.csdn.net/qq_26525215/article/details/53010442" target='_blank'>【Spring】基于IntelliJ IDEA搭建Maven</a> |
| 14 | + |
| 15 | +下面直接上示例吧。 |
| 16 | + |
| 17 | +#示例 |
| 18 | + |
| 19 | +##自定义事件 |
| 20 | + |
| 21 | +``` |
| 22 | +package cn.hncu.p2_5_2ApplicationEvent; |
| 23 | + |
| 24 | +import org.springframework.context.ApplicationEvent; |
| 25 | + |
| 26 | +/** |
| 27 | + * Created with IntelliJ IDEA. |
| 28 | + * User: 陈浩翔. |
| 29 | + * Date: 2016年11月15日. |
| 30 | + * Time: 下午 8:39. |
| 31 | + * Explain:自定义事件 |
| 32 | + */ |
| 33 | +public class DemoEvent extends ApplicationEvent{ |
| 34 | + private static final long serialVersionUID = 1L; |
| 35 | + private String msg; |
| 36 | + |
| 37 | + public DemoEvent(Object source,String msg) { |
| 38 | + super(source); |
| 39 | + this.msg=msg; |
| 40 | + System.out.println(this.getClass()+",构造方法"); |
| 41 | + } |
| 42 | + |
| 43 | + public String getMsg() { |
| 44 | + return msg; |
| 45 | + } |
| 46 | + |
| 47 | + public void setMsg(String msg) { |
| 48 | + this.msg = msg; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +``` |
| 53 | + |
| 54 | +##事件监听器 |
| 55 | + |
| 56 | +``` |
| 57 | +package cn.hncu.p2_5_2ApplicationEvent; |
| 58 | + |
| 59 | +import org.springframework.context.ApplicationListener; |
| 60 | +import org.springframework.stereotype.Component; |
| 61 | + |
| 62 | +/** |
| 63 | + * Created with IntelliJ IDEA. |
| 64 | + * User: 陈浩翔. |
| 65 | + * Date: 2016年11月15日. |
| 66 | + * Time: 下午 8:50. |
| 67 | + * Explain:事件监听器 |
| 68 | + */ |
| 69 | +@Component |
| 70 | +public class DemoListener implements ApplicationListener<DemoEvent> {//实现ApplicationListener接口,并指定监听的事件类型 |
| 71 | + @Override |
| 72 | + public void onApplicationEvent(DemoEvent demoEvent) {//使用onApplicationEvent方法对消息进行接受处理 |
| 73 | + String msg = demoEvent.getMsg(); |
| 74 | + System.out.println(this.getClass()+"监听到了bean-demoPublisher发布的消息:"+msg); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +``` |
| 79 | + |
| 80 | +##事件发布类 |
| 81 | + |
| 82 | +``` |
| 83 | +package cn.hncu.p2_5_2ApplicationEvent; |
| 84 | + |
| 85 | +import org.springframework.beans.factory.annotation.Autowired; |
| 86 | +import org.springframework.context.ApplicationContext; |
| 87 | +import org.springframework.stereotype.Component; |
| 88 | + |
| 89 | +/** |
| 90 | + * Created with IntelliJ IDEA. |
| 91 | + * User: 陈浩翔. |
| 92 | + * Date: 2016年11月15日. |
| 93 | + * Time: 下午 10:39. |
| 94 | + * Explain:事件发布类 |
| 95 | + */ |
| 96 | +@Component |
| 97 | +public class DemoPublisher { |
| 98 | + @Autowired |
| 99 | + ApplicationContext applicationContext;//注入ApplicationContext用来发布事件 |
| 100 | + |
| 101 | + public void publish(String msg){ |
| 102 | + DemoEvent demoEvent = new DemoEvent(this,msg); |
| 103 | + applicationContext.publishEvent(demoEvent);//在这里的时候,会去运行DemoListener中的onApplicationEvent方法 |
| 104 | + System.out.println("消息:"+demoEvent.getMsg()); |
| 105 | + //使用ApplicationContext的publishEvent方法来发布 |
| 106 | + } |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +``` |
| 111 | + |
| 112 | +##配置类 |
| 113 | + |
| 114 | +``` |
| 115 | +package cn.hncu.p2_5_2ApplicationEvent; |
| 116 | + |
| 117 | +import org.springframework.context.annotation.ComponentScan; |
| 118 | +import org.springframework.context.annotation.Configuration; |
| 119 | + |
| 120 | +/** |
| 121 | + * Created with IntelliJ IDEA. |
| 122 | + * User: 陈浩翔. |
| 123 | + * Date: 2016年11月15日. |
| 124 | + * Time: 下午 10:46. |
| 125 | + * Explain:配置类 |
| 126 | + */ |
| 127 | +@Configuration |
| 128 | +@ComponentScan("cn.hncu.p2_5_2ApplicationEvent") |
| 129 | +public class EventConfig { |
| 130 | +} |
| 131 | + |
| 132 | +``` |
| 133 | + |
| 134 | +##运行类 |
| 135 | + |
| 136 | +``` |
| 137 | +package cn.hncu.p2_5_2ApplicationEvent; |
| 138 | + |
| 139 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 140 | + |
| 141 | +/** |
| 142 | + * Created with IntelliJ IDEA. |
| 143 | + * User: 陈浩翔. |
| 144 | + * Date: 2016年11月15日. |
| 145 | + * Time: 下午 10:47. |
| 146 | + * Explain:运行类 |
| 147 | + */ |
| 148 | +public class Main { |
| 149 | + public static void main(String[] args) { |
| 150 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class); |
| 151 | + DemoPublisher demoPublisher = context.getBean(DemoPublisher.class); |
| 152 | + demoPublisher.publish("hello 你好..."); |
| 153 | + context.close(); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +``` |
| 158 | + |
| 159 | +#运行结果 |
| 160 | + |
| 161 | + |
| 162 | + |
0 commit comments