My Spring boot app has this application structure:
As provided in the 2nd answer above I was indeed using the spring boot, so removed the static keyword and that resolved my issue. Make sure you don't have static for the property defined with @Value.
in my case, the same problem had a slightly different cause - i had a class with a static instance variable (the old usual way we used to implement a Singleton pattern), into which i wanted to inject an entry from application.properties (probably the "static" thing was why the property could not be found - with no errors no anything :/).
moreover this class was in a totally different package from the class declared as @SpringBootApplication. i understood that package names and locations have to be taken into serious consideration with Spring Boot and automatic component scanning. my 5 cent
Probably not the exact solution you're looking for, but you can also declare a property source bean:
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html
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 service costructor
@Service
public class ApplicationBeansService implements IApplicationBeanService {
...
public ApplicationBeansService(Environment env){
...
String value = env.getProperty("your.value")
...
}
...
}
don't forget fill your application.properties:
your.value = some-string-value