java springboot 事件驱动开发最小示例

java springboot 事件驱动开发最小示例

项目结构

​

1. pom.xml(只保留最小依赖)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>event-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>event-demo</name>

<properties>
<java.version>17</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

2. 自定义事件 UserRegisterEvent.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.eventdemo.event;

import org.springframework.context.ApplicationEvent;

// 自定义事件:用户注册事件
public class UserRegisterEvent extends ApplicationEvent {
// 事件携带的数据
private final String username;

// 继承 ApplicationEvent 需要完成的
public UserRegisterEvent(Object source, String username) {
super(source);
this.username = username;
}
// 额外提供
public String getUsername() {
return username;
}
}

3. 事件发布 Service UserService.java

未来会在控制器里面使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.eventdemo.service;

import com.example.eventdemo.event.UserRegisterEvent;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor // 自动新建 ApplicationEventPublisher
public class UserService {

// Spring 内置事件发布器
private final ApplicationEventPublisher eventPublisher;

public void register(String username) {
System.out.println("[业务] 用户注册主逻辑:" + username);

// 发布事件
eventPublisher.publishEvent(new UserRegisterEvent(this, username));
}
}

4. 事件监听器 UserEventListener.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.eventdemo.listener;

import com.example.eventdemo.event.UserRegisterEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class UserEventListener {

// 监听 UserRegisterEvent
@EventListener
public void onUserRegister(UserRegisterEvent event) {
String username = event.getUsername();
System.out.println("[监听器] 收到注册事件,发送欢迎邮件给:" + username);
}

// 可以多个监听器监听同一个事件,互不影响
@EventListener
public void anotherListener(UserRegisterEvent event) {
System.out.println("[监听器2] 新增用户统计:" + event.getUsername());
}
}

5. 测试 Controller TestController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.example.eventdemo.controller;

import com.example.eventdemo.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class TestController {

private final UserService userService;

@GetMapping("/register")
public String register(@RequestParam String username) {
userService.register(username);
return "ok";
}
}

6. 启动类 EventDemoApplication.java

1
2
3
4
5
6
7
8
9
10
11
package com.example.eventdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EventDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EventDemoApplication.class, args);
}
}

测试

启动后访问:

1
http://localhost:8080/register?username=zhangsan

控制台输出顺序(同步):

1
2
3
[业务] 用户注册主逻辑:zhangsan
[监听器] 收到注册事件,发送欢迎邮件给:zhangsan
[监听器2] 新增用户统计:zhangsan

java springboot 事件驱动开发最小示例
https://taylorandtony.github.io/2026/10/30/java-springboot-事件驱动开发最小示例/
作者
TaylorAndTony
发布于
2026年10月30日
许可协议