Java 自移动到 Spring Boot 1.1.4.RELEASE 以来 @Value 和 application.properties 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24950395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issue with @Value and application.properties since moving to Spring Boot 1.1.4.RELEASE
提问by Zaheeb
I am having an issue since I moved to version 1.1.4.RELEASE of Spring Boot.
自从我迁移到 Spring Boot 的 1.1.4.RELEASE 版本后,我遇到了问题。
My variables that are annotated with @Value are presently not being populated with values despite them being present in the application.properties. Prior to this I was using Spring Boot @ version 1.0.2, and that was working fine.
我用@Value 注释的变量目前没有填充值,尽管它们存在于 application.properties 中。在此之前,我使用的是 Spring Boot @ 1.0.2 版,并且运行良好。
It all started since the upgrade, and I made no code change.
这一切都从升级开始,我没有更改代码。
SampleApplication.java
示例应用程序.java
package org.sample;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan
@EnableAutoConfiguration
@PropertySource(value = "classpath:application.properties")
public class SampleApplication {
private static Logger logger = LoggerFactory
.getLogger(TaskManagerApplication.class);
@Value("${org.sample.sampleProperty}")
private static String sampleProperty;
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
System.out.print("SampleApplication started: " + sampleProperty);
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
application.properties
应用程序属性
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/mydb
spring.datasource.username: root
spring.datasource.password: root
spring.datasource.driverClassName: com.mysql.jdbc.Driver
spring.jpa.show-sql: true
#Disable the ddl-auto:create once tables have been created
#spring.jpa.hibernate.ddl-auto: create
org.sample.sampleProperty=This is a sample property
photos.upload.dir=C:/temp/UserPhotos/
# Server port
server.port=8081
I have tried to add a PropertySourcesPlaceholderConfigurer bean and even PropertySourcesPlaceholderConfigurer but still the issue persists.
我尝试添加 PropertySourcesPlaceholderConfigurer bean 甚至 PropertySourcesPlaceholderConfigurer,但问题仍然存在。
Anyone experienced this ? Or is there a new way to load the properties file ?
有人经历过吗?或者有没有一种新的方法来加载属性文件?
Please note that my db connection and server port are being read properly since my application can connect to db and I have to access it through the specified port. sampleProperty variable remains null though.
请注意,我的数据库连接和服务器端口正在被正确读取,因为我的应用程序可以连接到数据库并且我必须通过指定的端口访问它。尽管如此,sampleProperty 变量仍然为空。
采纳答案by Maciej Walkowiak
@Value
is not meant to work on static fields- Properties from
application.properties
are available automatically without specifying@PropertySource
for it. - Instead of printing out property in
main()
method, you should do it after bean is constructed, for example by using@PostConstruct
@Value
不打算在静态字段上工作- 属性来自
application.properties
自动可用,无需指定@PropertySource
。 - 不要在
main()
方法中打印出属性,你应该在 bean 被构造之后进行,例如通过使用@PostConstruct
Fully working example:
完全工作的例子:
package demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
@Value("${org.sample.sampleProperty}")
private String sampleProperty;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@PostConstruct
public void postConstruct() {
System.out.print("SampleApplication started: " + sampleProperty);
}
}