• Spring 事件机制


    通过模拟邮件的发送,说明Spring的事件监听机制

    事件类

     1 package org.zln.module_chapter2.event;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.event.ApplicationContextEvent;
     5 
     6 /**
     7  * 发送邮件事件类
     8  * Created by sherry on 000005/7/5 22:10.
     9  */
    10 
    11 public class MailSendEvent extends ApplicationContextEvent {
    12 
    13     private String to;
    14     public MailSendEvent(ApplicationContext source,String to) {
    15         super(source);
    16         this.to = to;
    17     }
    18 
    19     public String getTo(){
    20         return this.to;
    21     }
    22 }
    D:GitHub oolsJavaEEDevelopLesson17_JavaWebDemosrcorgzlnmodule_chapter2eventMailSendEvent.java

    事件监听类

     1 package org.zln.module_chapter2.event;
     2 
     3 import org.springframework.context.ApplicationListener;
     4 
     5 /**
     6  * 邮件发送 监听类
     7  * Created by sherry on 000005/7/5 22:12.
     8  */
     9 
    10 public class MailSendListener implements ApplicationListener<MailSendEvent> {
    11 
    12     /*对MailSendEvent进行处理*/
    13     @Override
    14     public void onApplicationEvent(MailSendEvent mailSendEvent) {
    15         MailSendEvent mailSendEvent1 = mailSendEvent;
    16         System.out.println("MailSendListener:向"+mailSendEvent1.getTo()+"发送一封邮件");
    17     }
    18 }
    19     
    D:GitHub oolsJavaEEDevelopLesson17_JavaWebDemosrcorgzlnmodule_chapter2eventMailSendListener.java

    事件动作

     1 package org.zln.module_chapter2.event;
     2 
     3 import org.springframework.beans.BeansException;
     4 import org.springframework.context.ApplicationContext;
     5 import org.springframework.context.ApplicationContextAware;
     6 
     7 /**
     8  * 邮件发送类
     9  * Created by sherry on 000005/7/5 22:15.
    10  */
    11 public class MailSender implements ApplicationContextAware{
    12     private ApplicationContext applicationContext;
    13     @Override
    14     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    15         this.applicationContext = applicationContext;
    16     }
    17 
    18     public void sendMail(String to){
    19         System.out.println("MailSender:模拟发送邮件");
    20         MailSendEvent mailSendEvent = new MailSendEvent(applicationContext,to);
    21         /*向容器中所有事件监听器发送事件*/
    22         applicationContext.publishEvent(mailSendEvent);
    23     }
    24 }
    D:GitHub oolsJavaEEDevelopLesson17_JavaWebDemosrcorgzlnmodule_chapter2eventMailSender.java

    事件注册

        <!--事件-->
        <bean class="org.zln.module_chapter2.event.MailSendListener"/>
        <bean id="mailSender" class="org.zln.module_chapter2.event.MailSender"/>

    测试

    package org.zln.module_chapter2.event;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import static org.junit.Assert.*;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"/org/zln/cfg/spring/applicationContext.xml"})/*启动Spring容器*/
    public class MailSenderTest {
    
        @Autowired
        @Qualifier("mailSender")
        private MailSender mailSender;
    
        @Test
        public void testSetApplicationContext() throws Exception {
    
        }
    
        @Test
        public void testSendMail() throws Exception {
            mailSender.sendMail("nbzlnzlq@126.com");
        }
    }
    D:GitHub oolsJavaEEDevelopLesson17_JavaWebDemo estorgzlnmodule_chapter2eventMailSenderTest.java
  • 相关阅读:
    C# Log4.Net日志组件的应用系列(二)
    C# Log4.Net日志组件的应用系列(一)
    使用TFS+GIT实现分布式项目管理
    动软代码生成器使用教程
    SVN使用教程
    windows系统重装流程
    使用纯真IP库获取用户端地理位置信息
    使用扩展方法重写.NET底层架构
    使用单例模式创建模型仓储层的唯一调用
    使用SQL Delta.v5.1.1.98.破解版同步数据结构
  • 原文地址:https://www.cnblogs.com/sherrykid/p/4623144.html
Copyright © 2020-2023  润新知