
spring bean作用域: 引用 singleton:单例模式,当spring创建applicationContext容器的时候,spring会欲初始化所有的该作用域实例,加上lazy-init就可以避免预处理; 引用 prototype:原型模式,每次通过getBean获取该bean就会新产生一个实例,创建后spring将不再对其管理;
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: ${DEMO_APP_JDBC_URL:jdbc:mysql://127.0.0.1/demo?serverTimezone=Asia/Shanghai}
username: ${DEMO_APP_JDBC_USERNAME:demo}
password: ${DEMO_APP_JDBC_PASSWORD:demo}
hikari:
maximum-pool-size: ${DEMO_APP_JDBC_POOL_MAX_SIZE:30}
max-lifetime: ${DEMO_APP_JDBC_POOL_MAX_LIFE_TIME:60000}
minimum-idle: ${DEMO_APP_JDBC_POOL_MIN_SIZE:5}
配合docker 食用更香哦
@RestController
@RequestMapping("/api/v1/h5Adapter")
@Api(description = "server-h5-adapter")
public class BaMessageConverterController {
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableCircuitBreaker
@EnableCaching
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
//每天凌晨1点执行
@Scheduled(cron = "0 0 1 * * ?")
public void doTask() {
}
package com.aispeech.bj.bams.msproduct.config;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync(mode = AdviceMode.ASPECTJ)
public class ExecutorConfig {
/** Set the ThreadPoolExecutor's core pool size. */
private int corePoolSize = 10;
/** Set the ThreadPoolExecutor's maximum pool size. */
private int maxPoolSize = 300;
/** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */
private int queueCapacity = 10;
@Bean
public Executor myAsync() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix("MyExecutor-");
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
@Async("myAsync")
void notifyProductInfo(String thirdPartyId, ThirdPartyProductVO thirdPartyProductVO) {}
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
@RequestMapping("/test")
public void test(HttpServletRequest request) {
......
}
@Autowired private HttpServletRequest request; //自动注入request
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70006413/viewspace-2843909/ ,如需转载,请注明出处,否则将追究法律责任。