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