最近在新搭建一个小程序,用的是spring4.3.4,基于注解的方式配置spring
但我发现一个问题,在@Configuration的里面,@Value获取的属性不到值,
想可能是生命周期问题,于是参考了spring的官方文档,是使用Environment去获取配置值,到但是我发现Environment在@Bean里面并没有被注入,是空值
代码如下
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
/**
* 基于注解的Spring配置
*
* @author Kazz
*
*/
@Configuration
@PropertySource("file:config/config.properties") // 引入配置文件
@EnableAsync // 开启异步处理
@EnableScheduling // 开启作业调度
public class ApplicationConfigs {
final static private Logger logger = LoggerFactory.getLogger(ApplicationConfigs.class);
@Autowired
private Environment env;
/*
// 异步处理的线程池配置--------------------
@Value("${thread.corePoolSize:10}")
private int corePoolSizes;
@Value("${thread.maxPoolSize:200}")
private int maxPoolSize;
@Value("${thread.queueCapacity:1000}")
private int queueCapacity;
@Value("${thread.keepAliveSeconds:60}")
private int keepAliveSeconds;
// ----------------------------------
@Value("${appid}")
private String appId;*/
@Bean // 对配置文件读取的支持
public PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public Executor myExecutor() {
int corePoolSize = Integer.parseInt(env.getProperty("thread.corePoolSize")); //这里报空指针异常
int maxPoolSize = Integer.parseInt(env.getProperty("thread.maxPoolSize"));
int queueCapacity = Integer.parseInt(env.getProperty("thread.queueCapacity"));
int keepAliveSeconds = Integer.parseInt(env.getProperty("thread.keepAliveSeconds"));
logger.debug("看看各个线程池参数");
logger.debug("corePoolSizes {}",corePoolSize);
logger.debug("keepAliveSeconds {}",keepAliveSeconds);
logger.debug("queueCapacity {}",queueCapacity);
logger.debug("maxPoolSize {}",maxPoolSize);
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix("");
executor.setKeepAliveSeconds(keepAliveSeconds);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
主函数如下
import org.apache.log4j.xml.DOMConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import cn.com.agree.mqconsumer.config.ApplicationConfigs;
public class Main {
static{
DOMConfigurator.configure("./config/log4j.xml"); //指定日志配置文件
}
final static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfigs.class);
context.close();
logger.debug("测试写日志{}","12312321");
}
}
请问我这是遗漏了什么东西吗,才会导致@Configuration里的@Autowired Environment无法注入?