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 周行 閱讀(743) 評論(0)  編輯  收藏 所屬分類: IT技術
          Java-Android-jwebee
          主站蜘蛛池模板: 旺苍县| 和静县| 洪湖市| 齐齐哈尔市| 马龙县| 甘谷县| 普洱| 白沙| 大荔县| 禹州市| 巴林右旗| 红桥区| 宝坻区| 武陟县| 耿马| 扶绥县| 营口市| 广元市| 正定县| 成武县| 大荔县| 阿巴嘎旗| 德州市| 卓尼县| 玉龙| 乳源| 定西市| 永清县| 桃园市| 扎兰屯市| 罗源县| 安福县| 霍邱县| 集安市| 兰西县| 岳阳市| 利辛县| 和田市| 永善县| 栖霞市| 绥芬河市|