1.1 Application Event
-
Spring 4.1的写法请参考10点睛Spring4.1-Application Event
-
使用一个
@EventListener
取代了实现ApplicationListener
接口,使耦合度降低;
1.2 示例
- 包依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wisely</groupId>
<artifactId>spring4-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring4-2</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RC1</version>
</dependency>
</dependencies><repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
- 编写自定义的Application Event
package com.wisely.spring4_2.event;
import org.springframework.context.ApplicationEvent;
public class DemoEvent extends ApplicationEvent{
private static final long serialVersionUID = 1L;
private String msg;
public DemoEvent(Object source,String msg) {
super(source);
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
- 编写监听类
package com.wisely.spring4_2.event;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class DemoListener {
@EventListener //注意此处
public void handleDemoEvent(DemoEvent event){
System.out.println("我监听到了pulisher发布的message为:"+event.getMsg());
}
}
- 测试
package com.wisely.spring4_2.event;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.spring4_2.event");
Main main =context.getBean(Main.class);
main.pulish(context);
context.close();
}
public void pulish(AnnotationConfigApplicationContext context){
context.publishEvent(new DemoEvent(this, "22"));
}
}
输出结果
我监听到了pulisher发布的message为:22
相关推荐
Spring4 In Action-4.2-@AspectJ-切面,Spring4 In Action-4.2-@AspectJ-切面。Spring4 In Action-4.2-@AspectJ-切面
spring4.2,spring mvc,spring data,jpa,hibernate的程序构架
最新的spring 4.2主要JAR包,使用7-zip压缩
sts是一个定制版的Eclipse,专为Spring开发定制的,方便创建调试运行维护Spring应用。
压缩包内含有该文件所对应的URI,当开发环境没有网络或者远程获取速度过慢时能用到,希望能帮助到有需要的同学。
Spring4.2最新教学资源,高清动力节点,非常适合入门,链接长期有效,若失效请发送邮件 yhz9619@163.com
spring4.2完整jar
spring4.2与Hibernate4.3配置
相关spring框架的ioc容器中application的引入
spring4.2 mongodb 开发所需全部jar
spring约束-4.2.xsd
spring-webmvc-struts.jar对Struts和Spring整合时需要用到的包
spring开发所需要的jar包; 版本为4.2 经过实际测试。
org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....
spring-framework-4.2 完整代码
spring-core-4.2.1.RELEASE.jar
spring3.1官方所有的jar包 org.springframework.aop-3.1.RELEASE.jar org.springframework.asm-3.1.RELEASE.jar org.springframework.aspects-3.1.RELEASE.jar org.springframework.beans-3.1.RELEASE.jar org....
Spring4.2Mybatis3.3Spring4.2所需架包以及文件上传下载,Excel上传 下载所需架包
Spring-security4.2官方整合jar包,从官网下载整合出来的,其中还有demo示例与文档,包括了spring-security-core-4.2.2.RELEASE.jar,spring-security-web-4.2.2.RELEASE.jar等jar包。不想到官网找就可以在这里直接...
spring4.2+hibernate4.2+struts2.3.29整合所需jar包