欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring帶bean和config如何通過main啟動(dòng)測試

 更新時(shí)間:2023年07月20日 08:45:40   作者:多來哈米  
這篇文章主要介紹了spring帶bean和config,通過main啟動(dòng)測試,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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)文章

最新評(píng)論