spring带bean和config如何通过main启动测试
更新时间:2023年07月20日 08:45:40 作者:多来哈米
这篇文章主要介绍了spring带bean和config,通过main启动测试,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
main方法:
package com.xxx.tmp;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(final String[] args) {
final AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(SyncService.class);
final SyncService bean = applicationContext.getBean(SyncService.class);
// final SyncService bean = SpringContextHolder.getBean(SyncService.class);
for (int i = 0; i < 100; i++) {
bean.test(i);
}
}
}service方法:
package com.xxx.tmp;
import org.springframework.stereotype.Component;
@Component
public class SyncService {
// @Async
public void test(final int i) {
System.out.println(Thread.currentThread().getName() + "——" + i);
}
}配置:
package com.xxx.tmp;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
@ComponentScan("com.xxx.tmp")
public class AsyncConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
final ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(10);
threadPoolTaskExecutor.setMaxPoolSize(50);
threadPoolTaskExecutor.setQueueCapacity(50);
threadPoolTaskExecutor.setKeepAliveSeconds(1);
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new AsyncUncaughtExceptionHandler() {
@Override
public void handleUncaughtException(final Throwable throwable, final Method method, final Object... objects) {
System.out.println("出现异常啦~~~~~~");
}
};
}
}就可以真实启动了,无须通过test去测试
到此这篇关于spring带bean和config通过main启动测试的文章就介绍到这了,更多相关spring main启动测试内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
java.util.Random和concurrent.ThreadLocalRandom使用对比
这篇文章主要介绍了java.util.Random和concurrent.ThreadLocalRandom使用对比,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-07-07
Spring boot项目redisTemplate实现轻量级消息队列的方法
这篇文章主要给大家介绍了关于Spring boot项目redisTemplate实现轻量级消息队列的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧2019-04-04
一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程
使用SSM(Spring、SpringMVC和Mybatis)已经有段时间了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,下面这篇文章主要给大家介绍了关于整合SSM框架:Spring MVC + Spring + MyBatis的相关资料,需要的朋友可以参考下。2017-07-07
使用mybatis的interceptor修改执行sql以及传入参数方式
这篇文章主要介绍了使用mybatis的interceptor修改执行sql以及传入参数方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-12-12
SpringBoot通过redisTemplate调用lua脚本并打印调试信息到redis log(方法步骤详解)
这篇文章主要介绍了SpringBoot通过redisTemplate调用lua脚本 并打印调试信息到redis log,本文分步骤给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2020-02-02


最新评论