Reading values ββfrom application.properties Spring Boot
My Spring Boot Application has this application structure:
- CAC
- Main
- Java
- resources
- application.properties
- Main
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 requires the use of this language = java property. This is how I try to use it:
public class EntityManager {
@Value("${language}")
private static String newLang;
public EntityManager(){
System.out.println("langauge is: " + newLang);
}
}
This printable value is always "null" for some reason! I also tried to put this on top of the class declaration:
@PropertySource(value = "classpath:application.properties")
source to share
This can be achieved in several 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");
}
}
source to share
OK I figured it out with despair. I added
@PropertySource("classpath:application.properties")
but for some reason it still doesn't work.
Then I removed the "static" modifier and it works!
I'm not sure why it works without "static" presence, but it does. If I can explain this to me, that would be great because it is confusing.
source to share
Sometimes the classpath element for src/main/resources
contains an exclude tag. If application.properties
present in this list of excluded items, then @PropertySource("classpath:application.properties")
it will not be able to find the property file.
Either remove the [.classpath][1]
file exceptions entry for src/main/resources
manually, or use the Configure build path option in Eclipse and go to the Source tab. Then remove the entry to exclude from src
.
source to share
create you beans
@Configuration
@PropertySource("classpath:application.properties")
public class ApplicationBeansConfig {
@Autowired
Environment env;
@Bean
public IApplicationBeanService getService(){
return new ApplicationBeansService(env);
}
}
and then in the service costructor
@Service
public class ApplicationBeansService implements IApplicationBeanService {
...
public ApplicationBeansService(Environment env){
...
String value = env.getProperty("your.value")
...
}
...
}
don't forget to fill in your application.properties:
your.value = some-string-value
source to share
you can try to use @PropertySource
and give the path to the properties file, you can find an example below:
@Component
@PropertySource("classpath:application.properties")
public class EntityManager {
@Value("${language}")
private static String newLang;
public EntityManager(){
System.out.println("langauge is: " + newLang);
}
}
source to share