spring帶bean和config如何通過(guò)main啟動(dòng)測(cè)試
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)了,無(wú)須通過(guò)test去測(cè)試
到此這篇關(guān)于spring帶bean和config通過(guò)main啟動(dòng)測(cè)試的文章就介紹到這了,更多相關(guān)spring main啟動(dòng)測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決java web應(yīng)用線上系統(tǒng)偶發(fā)宕機(jī)的情況
這篇文章主要介紹了解決java web應(yīng)用線上系統(tǒng)偶發(fā)宕機(jī)的情況,具有好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
Java實(shí)現(xiàn)赫夫曼樹(shù)(哈夫曼樹(shù))的創(chuàng)建
給定N個(gè)權(quán)值作為N個(gè)葉子結(jié)點(diǎn),構(gòu)造一棵二叉樹(shù),若該樹(shù)的帶權(quán)路徑長(zhǎng)度(WPL)達(dá)到最小,稱(chēng)這樣的二叉樹(shù)為最優(yōu)二叉樹(shù),也稱(chēng)為哈夫曼樹(shù)(Huffman Tree)。這篇文章主要就是為大家介紹如何通過(guò)Java實(shí)現(xiàn)赫夫曼樹(shù),需要的朋友可以參考一下2021-12-12
Spring?Boot接口支持高并發(fā)具體實(shí)現(xiàn)代碼
這篇文章主要給大家介紹了關(guān)于Spring?Boot接口支持高并發(fā)具體實(shí)現(xiàn)的相關(guān)資料,在SpringBoot項(xiàng)目中通常我們沒(méi)有處理并發(fā)問(wèn)題,但是使用項(xiàng)目本身還是支持一定的并發(fā)量,需要的朋友可以參考下2023-08-08
mybatis?plus實(shí)現(xiàn)分頁(yè)邏輯刪除
這篇文章主要為大家介紹了mybatis?plus實(shí)現(xiàn)分頁(yè)邏輯刪除的方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
SpringBoot?隱式參數(shù)注入詳解告別重復(fù)代碼,讓?Controller?更優(yōu)雅
本文介紹了使用SpringBoot的HandlerMethodArgumentResolver實(shí)現(xiàn)隱式參數(shù)注入的技巧,通過(guò)自定義注解和解析器,可以簡(jiǎn)化Controller中的重復(fù)參數(shù)處理邏輯,提升代碼的可維護(hù)性和可讀性,感興趣的朋友跟隨小編一起看看吧2026-03-03
Java中FileWriter類(lèi)的簡(jiǎn)介說(shuō)明
這篇文章主要介紹了Java中FileWriter類(lèi)的簡(jiǎn)介說(shuō)明,FileWriter類(lèi)提供了多種寫(xiě)入字符的方法,包括寫(xiě)入單個(gè)字符、寫(xiě)入字符數(shù)組和寫(xiě)入字符串等,它還提供了一些其他的方法,如刷新緩沖區(qū)、關(guān)閉文件等,需要的朋友可以參考下2023-10-10
Netty源碼分析NioEventLoop線程的啟動(dòng)
這篇文章主要為大家介紹了Netty源碼分析NioEventLoop線程的啟動(dòng)示例,有需要的朋友,可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03

