vKazz 2017-04-09 14:45 采纳率: 20%
浏览 4081
已结题

spring的注解配置中,如何在@Bean方法中获取配置文件的值

最近在新搭建一个小程序,用的是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无法注入?

  • 写回答

3条回答 默认 最新

  • devmiao 2017-04-09 15:38
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码