1.@configuration
该注解声明一个配置类,相当于spring的一个xml配置文件中的beans,被@configuration打上注解的类中可包含多个@bean注解的内容,相当于xml配置文件中beans里边的内容。
package com.example.springboot.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* Created by ZhangPei on 2019/7/18.
*/
@Configuration
public class ThreadPoolConfiguration {
@Value("${threadpool.core-pool-size}")
private int corePoolSize;
@Value("${threadpool.max-pool-size}")
private int maxPoolSize;
@Value("${threadpool.queue-capacity}")
private int queueCapacity;
@Value("${threadpool.keep-alive-seconds}")
private int keepAliveSeconds;
@Bean
public ThreadPoolTaskExecutor threadPoolCreater() {
System.out.println("被bean注解打上的方法是不是会自动执行?");
ThreadPoolTaskExecutor threadPoolExecutor = new ThreadPoolTaskExecutor();
threadPoolExecutor.setCorePoolSize(this.corePoolSize);
threadPoolExecutor.setMaxPoolSize(this.maxPoolSize);
threadPoolExecutor.setQueueCapacity(queueCapacity);
threadPoolExecutor.setKeepAliveSeconds(this.keepAliveSeconds);
threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
return threadPoolExecutor;
}
//没打@bean注解的方法不会执行
public Thread getThread() {
System.out.println("没打bean注解会不会执行");
return null;
}
}
以上内容,在工程初始化的时候,被@bean注解的方法会自动执行,然后将返回值注入spring当做bean使用。