My Spring boot app has this application structure:
OK I figured it out with acts of desperation. I added the
@PropertySource("classpath:application.properties")
attribute but it still wasn't working for some reason.
I then deleted the "static" modifier and it worked!
I am not sure why it works without "static" being there but it does. If can explain it to me that would be wonderful because this is all confusing.
If you are using Spring boot you do not need @PropertySource("classpath:application.properties")
if you are using spring boot starter parent , just remove the static
keyword and it should start working.
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");
}
}
Missing stereotype annotation on top of class
@Component
public class EntityManager {
@Value("${language}")
private static String newLang;
public EntityManager(){
System.out.println("langauge is: " + newLang);
}
}
you can try to use @PropertySource
and give it the path to property file, you can find the sample below:
@Component
@PropertySource("classpath:application.properties")
public class EntityManager {
@Value("${language}")
private static String newLang;
public EntityManager(){
System.out.println("langauge is: " + newLang);
}
}
Sometimes, the classpath entry for src/main/resources
contains an exclude tag. If application.properties
is present in this list of excluded items then @PropertySource("classpath:application.properties")
will not be able to find the property file.
Either remove the exclude entry from [.classpath][1]
file for src/main/resources
manually, or use the Configure build path option in Eclipse and go to Source tab. Then remove the exclude entry from src
.