package com.muyu.test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; @Service public class UserRegistrationService { @Autowired private ApplicationEventPublisher eventPublisher; public void registerUser(String username) { // 注册新用户逻辑 // ... // 发布用户注册事件,这里使用 eventPublisher 将 预先定义好的事件交由 Spring 的Event Channel 管理 eventPublisher.publishEvent(new UserRegisteredEvent(this, username)); } }