一、核心结构: 定义事件捕捉类并实现ApplicationListener<泛型:表示要捕获的事件对象>
然后用@Component注解为此事件捕捉类创建对象,并纳入容器中。
二、项目结构:
三、项目代码:
1.监听器:
package com.zyq.event;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;
@Component
public class ContextStartEventHandler implements ApplicationListener<ContextStartedEvent> {
//此方法在容器启动时执行
public void onApplicationEvent(ContextStartedEvent contextStartedEvent) {
System.out.println("context started event received");
}
}
@Component
class CStopEventHandler implements ApplicationListener<ContextStoppedEvent> {
//此方法在容器停止运行时执行
public void onApplicationEvent(ContextStoppedEvent contextStoppedEvent) {
System.out.println("context stoped event received");
}
}
2.从spring容器中获取的对象:
package com.zyq.event;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class HelloWorld {
@Value("hello")
private String message;
}
3.测试类(注意:这个案例中去掉HelloWorld类,监听器也是可以工作的):
package com.zyq.test;
import com.zyq.event.HelloWorld;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCase {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("springmvc-servlet.xml");
// Let us raise a start event.
context.start();
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
System.out.println("helloWorldMsg:"+obj.getMessage());
// Let us raise a stop event.
context.stop();
}
}
4.spring核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- scan the package and the sub package -->
<context:component-scan base-package="com"/>
<!-- don't handle the static resource: 不拦截静态资源 -->
<mvc:default-servlet-handler/>
<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven/>
</beans>
5.pom.xml
<groupId>org.zyq</groupId>
<artifactId>4_springEventDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>4_springEventDemo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>