吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Spring_EventHandler

痞子三分冷 提交于 2020-02-27 07:16:07
<?xml version="1.0" encoding="GBK"?>
<project name="spring" basedir="." default="">
    <property name="src" value="src"/>
    <property name="dest" value="classes"/>

    <path id="classpath">
        <fileset dir="../../lib">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement path="${dest}"/>
    </path>

    <target name="compile" description="Compile all source code">
        <delete dir="${dest}"/>
        <mkdir dir="${dest}"/>
        <copy todir="${dest}">
            <fileset dir="${src}">
                <exclude name="**/*.java"/>
            </fileset>        
        </copy>
        <javac destdir="${dest}" debug="true" includeantruntime="yes"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${src}"/>
            <classpath refid="classpath"/>
            <compilerarg value="-Xlint:deprecation"/>
        </javac>
    </target>

    <target name="run" description="Run the main class" depends="compile">
        <java classname="lee.SpringTest" fork="yes" failonerror="true">
            <classpath refid="classpath"/>
        </java>
    </target>

</project>
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    <!-- 配置监听器 -->
    <bean class="org.crazyit.app.listener.EmailNotifier"/>
</beans>
package lee;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.crazyit.app.event.*;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class SpringTest
{
    public static void main(String[] args)
    {
        ApplicationContext ctx = new
            ClassPathXmlApplicationContext("beans.xml");
        // 创建一个ApplicationEvent对象
        EmailEvent ele = new EmailEvent("test" ,
            "spring_test@163.com" , "this is a test");
        // 发布容器事件
        ctx.publishEvent(ele);
    }
}
package org.crazyit.app.event;

import org.springframework.context.ApplicationEvent;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
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;
    }

    // address的setter和getter方法
    public void setAddress(String address)
    {
        this.address = address;
    }
    public String getAddress()
    {
        return this.address;
    }

    // text的setter和getter方法
    public void setText(String text)
    {
        this.text = text;
    }
    public String getText()
    {
        return this.text;
    }
}
package org.crazyit.app.listener;

import org.springframework.context.ApplicationListener;
import org.springframework.context.ApplicationEvent;

import org.crazyit.app.event.*;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class EmailNotifier implements ApplicationListener
{
    // 该方法会在容器发生事件时自动触发
    public void onApplicationEvent(ApplicationEvent evt)
    {
        // 只处理EmailEvent,模拟发送email通知...
        if (evt instanceof EmailEvent)
        {
            EmailEvent emailEvent = (EmailEvent)evt;
            System.out.println("需要发送邮件的接收地址  "
                + emailEvent.getAddress());
            System.out.println("需要发送邮件的邮件正文  "
                + emailEvent.getText());
        }
        else
        {
            // 其他事件不作任何处理
            System.out.println("其他事件:" + evt);
        }
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!