[英]Reading values from external configuration file returns null values
I have a configuration file config.properties
that contains the following content: 我有一个配置文件config.properties
,其中包含以下内容:
myFirstName='John'
myLastName='Doe'
I also have a very simple config class that simply outputs the values of the config.properties
file: 我还有一个非常简单的config类,该类仅输出config.properties
文件的值:
@Configuration
@ComponentScan(basePackages = {"com.boot.Training.*"})
@PropertySource("classpath:config.properties")
public class AppConfig {
@Value("${myFirstName}")
private static String myFirstName;
@Value("${myLastName}")
private static String myLastName;
public static void showVariables() {
System.out.println("firstName: " + myFirstName);
System.out.println("lastName: " + myLastName);
}
}
The problem is, when the values are output to the console, they appear null, even though they are obviously given a value in the config.properties
file.: 问题是,即使将值显然在config.properties
文件中提供了一个值,当将这些值输出到控制台时,它们也会显示为空:
2018-09-08 10:52:46.334 INFO 2787 --- [ main] c.b.Training.EnvironmentVariables.App : Started App in 2.542 seconds (JVM running for 3.084)
firstName: null
lastName: null
What am I missing here? 我在这里想念什么?
Your problem is about the static
keyword. 您的问题与static
关键字有关。 Because static fields initialization happens in many processor-cycles before Spring containers starts up . 因为静态字段初始化发生在Spring容器启动之前的许多处理器周期中。 So remove the static
keyword from your class variable ; 因此从类变量中删除 static
关键字;
@Value("${myFirstName}")
private String myFirstName;
@Value("${myLastName}")
private String myLastName;
EDIT : I added my code which is working correctly. 编辑:我添加了我的代码,它可以正常工作。 And my main class which is starting spring context like this ; 我的主要班级就是这样开始春天的;
public class AppMain {
public static void main(String args[]){
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
context.close();
}
}
So In my AppConfig.class
like this; 所以在我的AppConfig.class
是这样的;
@Configuration
@ComponentScan(basePackages = "com.example.spring")
@PropertySource(value = {"classpath:application.properties"})
public class AppConfig implements InitializingBean {
@Value("${firstKey}")
private String myFirstName;
@Value("${secondKey}")
private String myLastName;
public void showVariables() {
System.out.println("firstName: " + myFirstName);
System.out.println("lastName: " + myLastName);
}
public void afterPropertiesSet() throws Exception {
showVariables();
}
}
Dont care about implementing InitializingBean
. 不在乎实现InitializingBean
。 It provide us what do u want when bean was created. 它提供了当创建bean时您想要什么。 Like a postConstruct as well. 就像postConstruct一样。
Also my application.configuration
is ; 另外我的application.configuration
是;
firstKey = firsKeyValue
secondKey = secondKeyValue
Now when i am starting the app , i see this output ; 现在,当我启动该应用程序时,我看到此输出;
org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@37bba400: startup date [Sun Sep 09 11:37:35 EET 2018]; root of context hierarchy
firstName: firsKeyValue
lastName: secondKeyValue
It's working i think. 我认为它正在工作。 Apply your requirements and try once. 应用您的要求并尝试一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.