Java – from application Properties spring boot read value
•
Java
My spring launch application has the following application bindings
> src
>Main
>Java > resources
> 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
I have a class that needs to use the language = Java attribute That's how I try to use it:
public class EntityManager { @Value("${language}") private static String newLang; public EntityManager(){ System.out.println("langauge is: " + newLang); } }
For some reason, the print value is always "null"! I've also tried to put it on top of class declarations:
@PropertySource(value = "classpath:application.properties")
Solution
It can be implemented in many ways, see 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"); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码