Springboot 配置类( @Configuration) 不能使用@Value注解从application.yml中加载值
2018-01-19 11:12
726 查看
问题:
在Springboot应用中,通过Spring-context(版本4.3.6)的@Configuration注解配置类,使用@Value注解从application.yml配置文件中加载属性,但是总是报找不到,设置缺省值,则获取到的值总是缺省值。
1、Configuration文件
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ADemoConfiguration {
static Logger log = LoggerFactory.getLogger(ADemoConfiguration.class);
public ADemoConfiguration(){
log.info("Here ADemoConfiguration init...");
}
@Value("${yy.a.disabled:false}")
boolean disableA;
@Value("${eureka.client.serviceUrl.defaultZone:nothing}")
String defaultZone;
@Bean
AFileHandler aFileHandler(LoginHandlerHessianNoAuth auth) throws Exception{
AFileHandler udsFileHandler = new AFileHandler();
log.info("disableA:" + disableA + ",defaultZone:" + defaultZone);
aFileHandler.setDisabled(disableA);
aFileHandler.setClientConfigInfo(clientConfigInfo());
aFileHandler.setLoginHandler(auth);
return aFileHandler;
}2、yml文件yy:
a:
disabled: true
eureka:
client:
enabled: true
serviceUrl:
defaultZone: http://localhost:7070/eureka/解决:在配置文件ADemoConfiguration中加入: @Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}PropertySourcesPlaceholderConfigurer位置:
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
重新编译后,发现问题解决。
参考:
1、Spring @Value is not resolving to value from property file. https://stackoverflow.com/questions/15937592/spring-value-is-not-resolving-to-value-from-property-file
在Springboot应用中,通过Spring-context(版本4.3.6)的@Configuration注解配置类,使用@Value注解从application.yml配置文件中加载属性,但是总是报找不到,设置缺省值,则获取到的值总是缺省值。
1、Configuration文件
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ADemoConfiguration {
static Logger log = LoggerFactory.getLogger(ADemoConfiguration.class);
public ADemoConfiguration(){
log.info("Here ADemoConfiguration init...");
}
@Value("${yy.a.disabled:false}")
boolean disableA;
@Value("${eureka.client.serviceUrl.defaultZone:nothing}")
String defaultZone;
@Bean
AFileHandler aFileHandler(LoginHandlerHessianNoAuth auth) throws Exception{
AFileHandler udsFileHandler = new AFileHandler();
log.info("disableA:" + disableA + ",defaultZone:" + defaultZone);
aFileHandler.setDisabled(disableA);
aFileHandler.setClientConfigInfo(clientConfigInfo());
aFileHandler.setLoginHandler(auth);
return aFileHandler;
}2、yml文件yy:
a:
disabled: true
eureka:
client:
enabled: true
serviceUrl:
defaultZone: http://localhost:7070/eureka/解决:在配置文件ADemoConfiguration中加入: @Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}PropertySourcesPlaceholderConfigurer位置:
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
重新编译后,发现问题解决。
参考:
1、Spring @Value is not resolving to value from property file. https://stackoverflow.com/questions/15937592/spring-value-is-not-resolving-to-value-from-property-file
相关文章推荐
- spring boot:使用@ConfigurationProperties注解加载配置文件
- Spring Boot配置难得的使用解释说明(小猪佩奇:配置加载是有优先级的)
- SpringBoot学习之路:12.Spring Boot使用idea开发如何配置热加载
- 使用SpringBoot加载配置文件
- SpringBoot的配置文件加载顺序和使用方式
- 在Spring中使用@Configuration注解加载JavaConfig配置
- spring boot 读取配置文件问题,不能使用@Autowired
- Spring中使用classpath加载配置文件浅析
- spring-boot 加载本地静态资源文件路径配置
- Spring4.1使用c3p0加载配置文件连接数据库,Access denied for user 'root'@'localhost' 错误!
- spring-boot 加载本地静态资源文件路径配置
- spring boot 使用h2数据库配置(内存模式)
- 使用import简化spring的配置 spring import 标签的解析 使用import或加载spring配置时,报错误There is no ID/IDREF 多个Spring配置文件import resource路径配置
- Spring Boot 属性配置和使用
- spring-boot 加载本地静态资源文件路径配置
- Spring中使用classpath加载配置文件浅析
- Spring中使用classpath加载配置文件浅析
- Spring非XML方式配置@Configuration的使用。
- Web项目中使用XML配置加载Spring及Spring MVC的组件
- spring3.0 使用@value 加载配置文件property