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("出現(xiàn)異常啦~~~~~~");
? ? ? ? ? ? }
? ? ? ? };
? ? }
}就可以真實啟動了,無須通過test去測試
到此這篇關(guān)于spring帶bean和config通過main啟動測試的文章就介紹到這了,更多相關(guān)spring main啟動測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JDBC如何訪問MySQL數(shù)據(jù)庫,并增刪查改
這篇文章主要介紹了JDBC如何訪問MySQL數(shù)據(jù)庫,幫助大家更好的理解和學(xué)習(xí)java與MySQL,感興趣的朋友可以了解下2020-08-08
java.util.Random和concurrent.ThreadLocalRandom使用對比
這篇文章主要介紹了java.util.Random和concurrent.ThreadLocalRandom使用對比,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Spring boot項目redisTemplate實現(xiàn)輕量級消息隊列的方法
這篇文章主要給大家介紹了關(guān)于Spring boot項目redisTemplate實現(xiàn)輕量級消息隊列的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)詳細(xì)教程
使用SSM(Spring、SpringMVC和Mybatis)已經(jīng)有段時間了,項目在技術(shù)上已經(jīng)沒有什么難點了,基于現(xiàn)有的技術(shù)就可以實現(xiàn)想要的功能,下面這篇文章主要給大家介紹了關(guān)于整合SSM框架:Spring MVC + Spring + MyBatis的相關(guān)資料,需要的朋友可以參考下。2017-07-07
使用mybatis的interceptor修改執(zhí)行sql以及傳入?yún)?shù)方式
這篇文章主要介紹了使用mybatis的interceptor修改執(zhí)行sql以及傳入?yún)?shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot通過redisTemplate調(diào)用lua腳本并打印調(diào)試信息到redis log(方法步驟詳解)
這篇文章主要介紹了SpringBoot通過redisTemplate調(diào)用lua腳本 并打印調(diào)試信息到redis log,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

