Java-Android-jwebee
Java-Android-jwebee
对IT人来说,要成为一个优秀的技术型管理者,除了需要具备扎实的技术基础之外,还应该培养良好的人际关系能力、谈判与沟通技能、客户关系与咨询技能、商业头脑和财务技能以及创新意识,此外还要有巧妙的激励技巧和化解冲突与解决突发问题的能力.

      最近在研究Spring  感觉不错。。。。。   

      ApplicationContext中的事件处理是通过ApplicationEvent类和ApplicationListener接口来提供的。如果在上下文中部署一个实现了ApplicationListener接口的bean,那么每当一个ApplicationEvent发布到ApplicationContext时,这个bean就得到通知。

      以下是一个关于Spring事件的代码。


package com.xinnuo.spring.event;

/**
 * email 对象
 * @author 周行
 *
 */
public class Email {
 private String name;
 private String email;
 private String subject; 
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }

 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSubject() {
  return subject;
 }
 public void setSubject(String subject) {
  this.subject = subject;
 }

}


package com.xinnuo.spring.event;

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

/**
 * 事件监听器
 *
 * @author 周行
 *
 */
public class BlackListNotifier implements ApplicationListener {
 public void onApplicationEvent(ApplicationEvent event) {
  if (event instanceof BlackListEvent) {
   Email email = (Email)event.getSource();
   System.out.println("name:"+email.getName());
   System.out.println("email:"+email.getEmail());
   System.out.println("subject:"+email.getSubject());
  }
 }
}


package com.xinnuo.spring.event;

import org.springframework.context.ApplicationEvent;

/**
 * 事件
 *
 * @author 周行
 *
 */
public class BlackListEvent extends ApplicationEvent{
 private static final long serialVersionUID = 5866734089078382009L;

 public BlackListEvent(Object source) {
  super(source);
 }
}


package com.xinnuo.spring.event;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author 周行
 *
 */
public class EmailBean implements ApplicationContextAware {

    private List blackList;
    private ApplicationContext ctx;
    private Email email;
    public void setEmail(Email email) {
  this.email = email;
 }

 public void setBlackList(List blackList) {
        this.blackList = blackList;
    }
   
    public void setApplicationContext(ApplicationContext ctx) {
        this.ctx = ctx;
    }
    public void sendEmail(String address) {
        if (blackList.contains(address)) {
            BlackListEvent evt = new BlackListEvent(email);
            //发布事件
            ctx.publishEvent(evt);
            return;
        }
    }
}


package com.xinnuo.spring.event;

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

/**
 * Spring 事件测试
 * @author 周行
 *
 */
public class Demo {
 public static void main(String[] args) {
  ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
  EmailBean EmailBean = (EmailBean)context.getBean("emailer");
  EmailBean.sendEmail(black@list.org);
 }
}


applicationContext.xml

<beans>
 <bean id="emailer" class="com.xinnuo.spring.event.EmailBean" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
  <property name="blackList">
   <list>
    <value>black@list.org</value>
    <value>white@list.org</value>
    <value>john@doe.org</value>
   </list>
  </property>
  <property name="email">
   <ref bean="email" />
  </property>
 </bean>

 <bean id="blackListListener" class="com.xinnuo.spring.event.BlackListNotifier"></bean>

 <bean id="email" class="com.xinnuo.spring.event.Email" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
  <property name="name">
   <value>zhouxing</value>
  </property>
  <property name="email">
   <value>zx_zhouxing@sina.com</value>
  </property>
  <property name="subject">
   <value>你好,欢迎光临!</value>
  </property>
 </bean>
</beans>



jwebee

我的个人网站
posted on 2006-12-01 15:42 周行 阅读(739) 评论(0)  编辑  收藏 所属分类: IT技术

只有注册用户登录后才能发表评论。


网站导航:
 
Java-Android-jwebee