• SpringBoot在Configuration注解中使用@Value获取null的问题


    
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MyConfigure {
        @Value( "${spring.application.name}")
        private  String name ;
    
        @Value( "${spring.datasource.driver-class-name}")
        protected String driverClassName ;
        
        public MyConfigure(){
            // 这里 name 和 driverClassName 都是null
        }
    }

    修改 MyConfigure 实现 EnvironmentAware 接口

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.EnvironmentAware;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;
    
    @Configuration
    public class MyConfigure implements EnvironmentAware {
        @Value( "${spring.application.name}")
        private  String name ;
    
        @Value( "${spring.datasource.driver-class-name}")
        protected String driverClassName ;
        
        private Environment env;
    
        @Override
        public void setEnvironment(Environment environment) {
            this.env = environment; 
            this.doSomething();
        }
    
        public MyConfigure(){
            // 这里 name 和 driverClassName 都是null
        }
        
        private void doSomething(){
            // 这里 获取 name 和 driverClassName  
            this.driverClassName = this.env.getProperty("spring.datasource.driver-class-name");
        }
    }

    解决获取不到配置的问题




  • 相关阅读:
    chrome sarfri form.submit bug
    ruby首字母大写
    ubuntu 10.04 中文设置
    添加centos用户
    农行的短信提醒很滞后
    从github删除敏感数据
    从github删除敏感数据
    国内有不有做虚拟化或是叫云平台的朋友
    设置rails header编码
    centos5.5
  • 原文地址:https://www.cnblogs.com/Leechg/p/12331985.html
Copyright © 2020-2023  润新知