从application.properties Spring Boot读取值

我的Spring启动应用程序具有以下应用程序结

  • SRC
    • 主要
      • java的
      • 资源
        • application.properties

这是我的application.properties文件:

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 

我有一个类需要使用该语言= java属性。 这就是我尝试使用它的方式:

 public class EntityManager { @Value("${language}") private static String newLang; public EntityManager(){ System.out.println("langauge is: " + newLang); } } 

由于某种原因,该打印值始终为“null” ! 我也试过把它放在类声明之上:

 @PropertySource(value = "classpath:application.properties") 

它可以通过多种方式实现,请参阅下文。

 @Configuration @PropertySource("classpath:application.properties") public class EntityManager { @Value("${language}") private static String newLang; @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); } } 

要么

 @Configuration @PropertySource("classpath:application.properties") public class EntityManager { @Autowired private Environment env; public void readProperty() { env.getProperty("language"); } } 

如果您使用的是Spring引导,则不需要@PropertySource("classpath:application.properties")如果您使用的是spring boot starter parent,只需删除static关键字即可开始工作。

在类之上缺少构造型注释

 @Component public class EntityManager { @Value("${language}") private static String newLang; public EntityManager(){ System.out.println("langauge is: " + newLang); } } 

好吧我用绝望的行为弄明白了。 我加了

 @PropertySource("classpath:application.properties") 

属性,但由于某种原因它仍然无法正常工作。

然后我删除了“静态”修饰符并且它有效!

我不确定为什么它没有“静态”存在但它确实有效。 如果可以向我解释那将是美妙的,因为这一切都令人困惑。

有时, src/main/resources的类路径条目包含exclude标记。 如果application.properties存在于此排除项列表中,则@PropertySource("classpath:application.properties")将无法找到属性文件。

手动从src/main/resources [.classpath][1]文件中删除exclude条目,或者使用Eclipse中的Configure build path选项并转到Source选项卡。 然后从src删除exclude条目。

创造你的豆子

 @Configuration @PropertySource("classpath:application.properties") public class ApplicationBeansConfig { @Autowired Environment env; @Bean public IApplicationBeanService getService(){ return new ApplicationBeansService(env); } } 

然后在服务costructor中

 @Service public class ApplicationBeansService implements IApplicationBeanService { ... public ApplicationBeansService(Environment env){ ... String value = env.getProperty("your.value") ... } ... } 

不要忘记填写你的application.properties:

 your.value = some-string-value 

您可以尝试使用@PropertySource并为其提供属性文件的路径,您可以在下面找到示例:

 @Component @PropertySource("classpath:application.properties") public class EntityManager { @Value("${language}") private static String newLang; public EntityManager(){ System.out.println("langauge is: " + newLang); } } 

可能不是您正在寻找的确切解决方案,但您也可以声明属性源bean:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html

在我的情况下,同样的问题有一个稍微不同的原因 – 我有一个静态实例变量的类(我们用来实现Singleton模式的旧方法),我想从application.properties中注入一个条目(可能“静态”的原因是无法找到属性的原因 – 没有错误没有任何东西:/)。

此外,这个类与声明为@SpringBootApplication的类完全不同。 我知道Spring Boot和自动组件扫描必须认真考虑包名和位置。 我的5美分