当前位置: 首页> 科技> 互联网 > windows优化大师有用吗_计算机网络毕业设计论文_seo排名技巧_网络推广有前途吗

windows优化大师有用吗_计算机网络毕业设计论文_seo排名技巧_网络推广有前途吗

时间:2025/7/10 3:47:29来源:https://blog.csdn.net/qq_41984644/article/details/142660915 浏览次数:4次
windows优化大师有用吗_计算机网络毕业设计论文_seo排名技巧_网络推广有前途吗
  1. 定义事件类
/*
* 1. 定义事件类
* 首先,我们创建一个自定义事件 UserRegisteredEvent,用于表示用户注册事件。
* */
public class UserRegisteredEvent extends ApplicationEvent {private final String email;public UserRegisteredEvent(Object source, String email) {super(source);this.email = email;}public String getEmail() {return email;}
}
  1. 创建事件发布器
@Service
public class UserService {private final ApplicationEventPublisher publisher;public UserService(ApplicationEventPublisher publisher) {this.publisher = publisher;}public void registerUser(String email) {// 这里可以添加用户注册逻辑,比如保存到数据库System.out.println("User registered: " + email);/** 2. 创建事件发布器* 我们创建一个服务类 UserService,* 在用户注册时发布 UserRegisteredEvent。* */// 发布用户注册事件publisher.publishEvent(new UserRegisteredEvent(this, email));}
}
  1. 创建事件监听器
/*
* 3. 创建事件监听器
* 然后,我们创建一个监听器 EmailService,在收到 UserRegisteredEvent 时发送欢迎邮件。
* */
@Component
public class EmailService {@EventListenerpublic void onUserRegistered(UserRegisteredEvent event) {String email = event.getEmail();// 这里模拟发送邮件System.out.println("Sending welcome email to: " + email);}
}

4、启动类

@SpringBootApplication
public class MytestApplication implements CommandLineRunner {@Autowiredprivate UserService userService;public static void main(String[] args) {SpringApplication.run(MytestApplication.class, args);}@Overridepublic void run(String... args) {// 模拟用户注册userService.registerUser("user@example.com");}
}
关键字:windows优化大师有用吗_计算机网络毕业设计论文_seo排名技巧_网络推广有前途吗

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: