昨天在調程序時出現標題上的異常
查了下好像是說SecurityManager相關問題
后來我看到代碼中:
@Bean(name = "securityManager") public SecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); //設置自定義realm. securityManager.setRealm(getDatabaseRealm()); //配置記住我 securityManager.setRememberMeManager(rememberMeManager()); SecurityUtils.setSecurityManager(securityManager); return securityManager; }
SecurityUtils.setSecurityManager(securityManager)這一行之前被注釋掉了
然后看到SecurityUtils里面
public static SecurityManager getSecurityManager() throws UnavailableSecurityManagerException { SecurityManager securityManager = ThreadContext.getSecurityManager(); if (securityManager == null) { securityManager = securityManager; } if (securityManager == null) { String msg = "No SecurityManager accessible to the calling code, either bound to the " + ThreadContext.class.getName() + " or as a vm static singleton. This is an invalid application configuration."; throw new UnavailableSecurityManagerException(msg); } else { return securityManager; } }
getSecurityManager()正是拋出的該異常
在開頭的SecurityManager加入該行異常消失
SecurityUtils.setSecurityManager(securityManager);
后來我想原因可能出現在我在之前代碼更改加入了對控制層某方法多線程的處理以及配置:
package com.tansuo365.test1.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.task.TaskExecutor; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; @Configuration @EnableAsync public class AsyncConfig { @Bean public TaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); // 設置核心線程數 executor.setCorePoolSize(5); // 設置最大線程數 executor.setMaxPoolSize(10); // 設置隊列容量 executor.setQueueCapacity(20); // 設置線程活躍時間(秒) executor.setKeepAliveSeconds(60); // 設置默認線程名稱 executor.setThreadNamePrefix("danhao-"); // 設置拒絕策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 等待所有任務結束后再關閉線程池 executor.setWaitForTasksToCompleteOnShutdown(true); return executor; } }
@Async @RequestMapping("/getChukuNumber") public ListenableFuture<String> genBillCode(String type) throws Exception { StringBuffer billCodeStr = new StringBuffer(); billCodeStr.append(chukudanPrefix); billCodeStr.append(DateUtil.getCurrentDateStr()); String todayMaxChukuDanNumber = chukuZongService.getTodayMaxChukuDanNumber(); if (todayMaxChukuDanNumber != null) { billCodeStr.append(StringUtil.formatCode(todayMaxChukuDanNumber)); } else { billCodeStr.append("0001"); } return new AsyncResult<>(billCodeStr.toString()); // return billCodeStr.toString(); }
如果要啟用還要在springboot上加注注解:
//@EnableCaching @SpringBootApplication @MapperScan(value = {"com.xxxxxxx.test1.mapper"}) @EnableAsync//開啟異步任務 public class Test1Application { public static void main(String[] args) { SpringApplication.run(Test1Application.class, args); } }
問題結束
