📡 Spring 事件机制

🧪 实验任务卡(事件)

🎉 事件发布演示

模拟用户登录事件,观察事件发布和监听过程

等待事件发布...

🔄 事件机制流程

发布者 ApplicationEventPublisher 事件 监听者
核心优势:

💻 代码实现

1. 定义事件

public class UserEvent {
    public enum Type { CREATED, UPDATED, DELETED, LOGIN }
    
    private Type type;
    private Long userId;
    private String userName;
    private LocalDateTime timestamp;
    
    // constructor, getters...
}

2. 发布事件

@Component
public class UserEventPublisher {
    
    @Autowired
    private ApplicationEventPublisher eventPublisher;
    
    public void publishUserLogin(Long userId, String userName) {
        UserEvent event = new UserEvent(
            UserEvent.Type.LOGIN,
            userId,
            userName,
            "用户登录成功"
        );
        eventPublisher.publishEvent(event);
    }
}

3. 监听事件

@Component
public class UserEventListener {
    
    // 基础监听
    @EventListener
    public void handleUserEvent(UserEvent event) {
        System.out.println("收到事件: " + event.getType());
    }
    
    // 条件监听 - 只处理登录事件
    @EventListener(condition = "#event.type == T(com.example.demo.model.UserEvent$Type).LOGIN")
    public void handleLogin(UserEvent event) {
        System.out.println("用户登录: " + event.getUserName());
    }
    
    // 异步监听 - 不阻塞主流程
    @Async
    @EventListener
    public void sendWelcomeEmail(UserEvent event) {
        // 发送邮件...
    }
}

4. 控制器中使用

@RestController
@RequestMapping("/aop")
public class AopEventController {
    
    @Autowired
    private UserEventPublisher eventPublisher;
    
    @PostMapping("/event/publish")
    public Map<String, Object> publishEvent(
            @RequestParam Long userId,
            @RequestParam String userName) {
        
        // 发布事件
        eventPublisher.publishUserLogin(userId, userName);
        
        return Map.of(
            "message", "事件已发布",
            "userId", userId,
            "userName", userName
        );
    }
}

🎯 应用场景

场景事件类型处理逻辑
用户注册UserCreatedEvent发送欢迎邮件、初始化数据
订单创建OrderCreatedEvent扣库存、发送通知
支付成功PaymentSuccessEvent更新订单状态、发送短信
用户登录UserLoginEvent记录登录日志、更新在线状态

💡 最佳实践

← 返回学习中心