从application.properties Spring Boot读取值 [英] Reading values from application.properties Spring Boot
本文介绍了从application.properties Spring Boot读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
My Spring启动应用程序具有以下应用程序结构:
My Spring boot app has this application structure:
- src
- main
- java
- 资源
- application.properties
这是我的application.properties文件:
This is my application.properties file:
logging.level.org.springframework=TRACE logging.level.org.hibernate=ERROR spring.resources.chain.strategy.content.enabled=true spring.resources.chain.strategy.content.paths=/** #spring.resources.chain.cache=false #spring.resources.chain.html-application-cache=false #spring.headers.cache=false language=java
我有一个类需要使用该language = java属性。这就是我尝试使用它的方式:
I have a class which requires the use of that language=java property. This is how I am trying to use it:
public class EntityManager { @Value("${language}") private static String newLang; public EntityManager(){ System.out.println("langauge is: " + newLang); } }
打印的值始终为null 出于某种原因!我也试过把它放在类声明之上:
That printed value is always "null" for some reason! I have also tried putting this on top of the class declaration:
@PropertySource(value = "classpath:application.properties")
推荐答案
可以通过多种方式实现,请参阅下文。
It can be achieved in multiple ways, refer below.
@Configuration @PropertySource("classpath:application.properties") public class EntityManager { @Value("${language}") private static String newLang; @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); } }
OR
@Configuration @PropertySource("classpath:application.properties") public class EntityManager { @Autowired private Environment env; public void readProperty() { env.getProperty("language"); } }
这篇关于从application.properties Spring Boot读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- main
查看全文