I'd like to create a shared project jar, that has some services. These service should make use of a properties file.
Ideally I just want to make use of the services lateron when I add the shared jar as a dependency to other projects. I don't want to make any further configuration like importing the shared property file or so.
Within the shared jar, I'd like to inject the properties using Spring
. But how can I do this?
project-commons/src/main/java:
@Service
public class MyService {
@Value("${property.value}") private String value;
public String getValue() {
return value;
}
}
project-commons/src/main/resources/application.properties:
property.value=test
project-web/src/main/java:
@Component
public class SoapService {
@Autowired
private MyService service;
//should return "test"
public String value() {
return service.getValue();
}
}
When I run it:
Illegal character in path at index 1: ${property.value}
So, the propertyfile is not resolved. But how can I tell spring to automatically use it when using the appropriate service?
Thanks to the comment of Hank Lapidez, adding the following statement fixes the problem:
@Configuration
@PropertySource("classpath:appdefault.properties")
public CommonConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With