Spring 框架通过其丰富的注解如@EnableAsync和@Async,为应用提供了强大的异步处理能力,允许开发者轻松地将方法调用异步化,有效提升应用的响应性和吞吐量。同时,事件发布和监听机制,以@EventLog和@EventListener注解为代表,使得应用组件之间的解耦和交互更加灵活。这些注解的结合使用,为构建复杂而高效的企业级应用提供了强有力的支持。
肖哥弹架构 跟大家“弹弹” 框架注解使用,需要代码关注
欢迎 点赞,关注,评论。
关注公号 Solomon 肖哥弹架构获取更多精彩内容
历史热点文章
1、异步执行
@EnableAsync
@EnableAsync 注解用于在配置类上开启对异步方法的支持。
无属性。
@Configuration@EnableAsyncpublic class AsyncConfig { // 配置异步相关的Bean}
复制代码
@Async
@Async 注解用于声明一个方法为异步执行的方法。
@Servicepublic class AsyncService {
@Async("myAsyncExecutor") public void performAsyncTask() { // 异步执行的逻辑 }}
复制代码
2、事件发布与监听
@EnableEventLog
@EnableEventLog 注解用于开启事件的日志记录功能(如果有自定义实现)。
无属性。
@Configuration@EnableEventLogpublic class EventLogConfig { // 配置事件日志相关的Bean}
复制代码
@EventLog
@EventLog 注解用于声明一个方法在执行后发布一个事件。
注解属性介绍
value: 指定事件的类型。
注解业务案例
public class EventService { @EventLog("userCreatedEvent") public void registerUser(User user) { // 用户注册逻辑 }}
复制代码
@EventListener
@EventListener 注解用于声明一个方法为事件监听器。
public class EventConsumer {
@EventListener(UserCreatedEvent.class) public void onUserCreated(UserCreatedEvent event) { // 处理用户创建事件 }}
复制代码
3、条件化配置
@Conditional
@Conditional 注解用于根据条件包含配置。
注解属性介绍
value: 指定条件类。
注解业务案例
public class MyConditionalConfig {
@Bean @Conditional("#{beanFactory.containsBean('someBean')}") @Conditional(MyCustomCondition.class) public SomeBean someBean() { return new SomeBean(); }}
复制代码
4、异步注解综合案例
import org.springframework.aop.framework.autoproxy.AsyncAnnotationBeanPostProcessor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.Async;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.annotation.AsyncConfigurer;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import org.springframework.stereotype.Service;
import java.util.concurrent.Callable;import java.util.concurrent.Future;import java.util.concurrent.ThreadPoolExecutor;
// 配置类,用于开启异步支持和自定义线程池@Configuration@EnableAsyncpublic class AsyncConfig implements AsyncConfigurer {
// 定义异步任务执行器 @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); // 设置核心线程数 executor.setMaxPoolSize(5); // 设置最大线程数 executor.setQueueCapacity(10); // 设置队列容量 executor.setThreadNamePrefix("Async-Thread-"); // 设置线程名前缀 executor.initialize(); return executor; }
// 异常处理器,用于处理异步方法抛出的异常 @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new AsyncUncaughtExceptionHandler() { @Override public void handleUncaughtException(Throwable ex, Method method, Object... params) { System.out.println("Async method " + method.getName() + " threw an exception: " + ex.toString()); } }; }}
// 业务服务类,包含异步方法@Servicepublic class AsyncService {
// 异步方法,无返回值 @Async public void processOrder(Order order) { try { // 耗时的订单处理过程 Thread.sleep(3000); System.out.println("Order processed in thread: " + Thread.currentThread().getName()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }
// 异步方法,有返回值,返回Future对象 @Async public Future<String> fetchDataFromRemoteService() { try { //远程服务获取数据的过程 Thread.sleep(2000); return new AsyncResult<>("Data fetched successfully"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return new AsyncResult<>(null); } }}
// 订单实体类class Order { // 订单相关信息}
// 应用程序主入口public class AsyncApplication {
public static void main(String[] args) { // 启动Spring应用上下文 }}
复制代码
AsyncConfig 类通过实现 AsyncConfigurer 接口来配置异步任务执行器(线程池)和异步方法的异常处理器。
AsyncService 类中的 processOrder 方法和 fetchDataFromRemoteService 方法被标记为 @Async,表示它们将在异步执行器配置的线程池中异步执行。
processOrder 方法了处理订单的耗时操作,而 fetchDataFromRemoteService 方法从远程服务获取数据的操作,并返回 Future 对象。
异步方法抛出的异常将由 AsyncConfig 类中的异常处理器捕获并记录。
5、事件注解综合案例
1. 定义事件
首先定义一个应用事件UserRegisteredEvent,它扩展了 Spring 的ApplicationEvent类。
import org.springframework.context.ApplicationEvent;public class UserRegisteredEvent extends ApplicationEvent { private User user;
public UserRegisteredEvent(User user) { super(user); // 将用户信息作为事件的来源 this.user = user; }
public User getUser() { return user; }}
复制代码
2. 创建事件发布方法
在用户服务UserService中,创建一个方法来发布UserRegisteredEvent事件。
import org.springframework.context.ApplicationContext;import org.springframework.stereotype.Service;
@Servicepublic class UserService {
private final ApplicationContext applicationContext;
public UserService(ApplicationContext applicationContext) { this.applicationContext = applicationContext; }
public void registerUser(User user) { // 注册用户逻辑 applicationContext.publishEvent(new UserRegisteredEvent(user)); }}
复制代码
3. 使用@EventListener监听事件
然后,我们创建一个邮件发送服务EmailService,它将监听UserRegisteredEvent事件并发送邮件。
import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;
@Servicepublic class EmailService {
@EventListener @Async // 异步发送邮件,提高性能 public void onUserRegistered(UserRegisteredEvent event) { User user = event.getUser(); // 发送欢迎邮件逻辑 sendWelcomeEmail(user); }
private void sendWelcomeEmail(User user) { // 实际发送邮件的代码 }}
复制代码
4. (可选)使用@EventLog记录事件
如果我们想要记录所有用户注册事件,可以创建一个自定义的@EventLog注解,并在事件监听器中使用它。
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface EventLog { String value();}
@Servicepublic class LoggingService {
@EventLog("userRegistered") public void logUserRegistration(UserRegisteredEvent event) { // 记录事件到日志系统的逻辑 logRegistration(event.getUser()); }
private void logRegistration(User user) { // 实际记录日志的代码 }}
复制代码
请注意,@EventLog是一个自定义注解,Spring 框架本身并不提供此注解。在实际应用中,您可能需要实现自定义的日志记录逻辑。
5. 配置 Spring 以支持异步事件监听器
最后,确保 Spring 配置支持异步事件监听器,这可以通过在配置类上添加@EnableAsync注解来实现。
import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableAsync;
@Configuration@EnableAsyncpublic class AsyncConfig { // 异步配置}
复制代码
评论