2023年12月22日
springboot项目的pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>even</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>even</name>
<description>even</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.7.6</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.even.EvenApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
springboot项目的启动类配置
@SpringBootApplication
@EnableAsync
public class EvenApplication {
public static void main(String[] args) {
SpringApplication.run(EvenApplication.class, args);
}
}
事件类
/**
* @author HK
*/
public class Student extends ApplicationEvent {
private String name;
private Integer age;
public Student(Object source,String name,Integer age) {
super(source);
this.age=age;
this.name=name;
}
}
监听器两个
/**
* @author HK
*/
@Component
public class StudentListener1 implements ApplicationListener<Student> {
@Override
@Async
public void onApplicationEvent(Student event) {
System.out.println("监视器1被触发");
}
}
/**
* @author HK
*/
@Component
public class StudentListener2 implements ApplicationListener<Student> {
@Override
@Async
public void onApplicationEvent(Student event) {
System.out.println("监视器2被触发");
}
}
发布者1个
/**
* @author HK
*/
@Component
public class StudentPublisher {
private final ApplicationContext applicationContext;
@Autowired
public StudentPublisher(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public void publish(String message,Integer id) {
applicationContext.publishEvent(new Student(this, message,id));
}
}
controller层
/**
* @author HK
*/
@RestController
@RequestMapping("user")
public class event {
@Autowired
private StudentPublisher studentPublisher;
@GetMapping("/point")
public void register(String userName, Integer point) {
studentPublisher.publish(userName, point);
}
}
访问路径:
结果
到此结束