Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17489
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 211
  • 用 户 组: 普通用户
  • 注册时间: 2015-12-09 16:47
文章分类

全部博文(21)

文章存档

2017年(1)

2016年(9)

2015年(11)

我的朋友
最近访客

分类: Java

2015-12-18 08:40:05

相比前两大框架,spring的使用就简单的多,导入spring相关依赖包后,在javase中可以通过以下代码获得

点击(此处)折叠或打开

  1. ApplicationContext appl = new ClassPathXmlApplicationContext("bean.xml");
  2.         System.out.printf(appl+"\n");
  3.         News new2=(News) appl.getBean("news");//通过使用getBean获取代理对象的代理类

  4.         //spring根据配置文件信息,负责创建News类,并且为News的相关属性赋值-这种由spring为对象赋值的叫做

  5.         //控制反转即(Inversion of control ioc)

  6.         System.out.printf(new2.getContent()+"\n");

对应的bean配置文件如下

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns=""
  3.        xmlns:xsi=""
  4.        xmlns:aop=""
  5.        xmlns:tx=""
  6.        xmlns:context=""
  7.        xsi:schemaLocation="
  8.                            /spring-beans-2.5.xsd
  9.                            
  10.                            /spring-aop-2.5.xsd
  11.                            
  12.                            /spring-context-2.5.xsd
  13.                            
  14.                            /spring-tx-2.5.xsd">
  15.    <bean id="news" class="model.News">
  16.            <property name="content" value="12312"></property>
  17.    </bean>
  18.    <bean id="emailListener" class="model.EmailListener">
  19.    
  20.    </bean>
  21.     <!-- <import resource="applicationContext-db.xml"/>
  22.        <import resource="applicationContext-person.xml"/> -->
  23. </beans>

spring对外提供了很多接口,并且以观察者的模式进行监听。首先时间发生源要继承ApplicationEvent对象,然后事件监听器要实现ApplicationListener,在应用的时候只要调用ApplicationContext.pushEvent();就可以触发事件的相应。以下是源代码

点击(此处)折叠或打开

  1. package model;

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

  4. public class EmailListener implements ApplicationListener {

  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent applicationevent) {
  7.         // TODO Auto-generated method stub

  8.         if(applicationevent instanceof EmailEvent){
  9.             EmailEvent evt =(EmailEvent)applicationevent;
  10.             System.out.printf("需要发邮件的接收地址:"+evt.getName());
  11.             
  12.         }else{
  13.             
  14.             System.out.printf("容器内部的事件");
  15.         }
  16.         
  17.     }

  18. }


点击(此处)折叠或打开

  1. package model;

  2. import org.springframework.context.ApplicationEvent;

  3. public class EmailEvent extends ApplicationEvent {
  4.     private String name;
  5.     private String address;
  6.     /**
  7.      * application 中的事件机制是以观察者模式运行的,首先实现
  8.      *继承ApplicationEvent中
  9.      * @param source
  10.      */
  11.     public EmailEvent(Object source) {
  12.         super(source);
  13.         // TODO Auto-generated constructor stub

  14.     }
  15.     public EmailEvent(Object source,String name,String address){
  16.         super(source);
  17.         this.name=name;
  18.         this.address=address;
  19.         
  20.     }
  21.     public String getName() {
  22.         return name;
  23.     }
  24.     public void setName(String name) {
  25.         this.name = name;
  26.     }
  27.     public String getAddress() {
  28.         return address;
  29.     }
  30.     public void setAddress(String address) {
  31.         this.address = address;
  32.     }

  33. }

触发代码

点击(此处)折叠或打开

  1. EmailEvent evet = new EmailEvent("123","12313","123123");
  2.         appl.publishEvent(evet);

阅读(187) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~