Reputation: 373
I have a Spring Boot application. I have a configuration service application which provides all configurations for my application. I created a client which pulls all settings for the application and puts them into the context. I created java class which does this job:
@Configuration
public class ContextConfiguration {
@PostConstruct
public void getContextConfiguration(){
ConfigServiceResponse configurations = configurationServiceClient.getConfigurations(configurationEnv);
Properties properties = generateProperties(configurations.getConfigParameterList());
MutablePropertySources propertySources = env.getPropertySources();
propertySources.addFirst(new PropertiesPropertySource("APPLICATION_PROPERTIES", properties));
}
}
Also I created java class for configuration of DataSource:
@Configuration
public class PersistentConfiguration {
@Value("${db.url}")
private String serverDbURL;
@Value("${db.user}")
private String serverDbUser;
@Value("${db.password}")
private String serverDbPassword;
public DataSource dataSource() {
return new SingleConnectionDataSource(serverDbURL, serverDbUser, serverDbPassword, true);
}
}
With this configuration the App worked well. Until I migrated to Spring Data. I just added dependency to the Gradle configuration:
compile("org.springframework.boot:spring-boot-starter-data-jpa")
After I added dependency, I can see an exception while the app is starting:
Error creating bean with name 'persistentConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'db.url' in value "${db.url}
If I remove dependency the App starts with no issues.
And the previous class which configures client and calls it to get data even was not invoked.
Upvotes: 0
Views: 446
Reputation: 81988
In order to let Spring know it should process your ContextConfiguration
first, add a DependsOn
annotation to the PersistentConfiguration
like so:
@DependsOn("contextConfiguration")
@Configuration
public class PersistentConfiguration {
...
The problem is that nothing in your PersistentConfiguration
tells Spring that it depends on the ContextConfiguration
, although it does because the former uses variables only initialized by the latter.
This is exactly what DependsOn
is for. From the JavaDoc:
Any beans specified are guaranteed to be created by the container before this bean. Used infrequently in cases where a bean does not explicitly depend on another through properties or constructor arguments, but rather depends on the side effects of another bean's initialization.
Upvotes: 1