I started to change a couple of projects from xml configuration to annotations and it wasn't trivial.
One of these items was the injection of properties using annotations.
First of all, we need a property configurer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean id="propertyConfigurer" | |
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> | |
<property name="location"> | |
<value>file:${configuration.path}/ng.properties</value> | |
</property> | |
</bean> |
The configuration file is a common property file.
application.title = My Application Title
In my case, I want to define a configuration bean, so I put in one place all the configurations. But you can use the same approach injecting the properties in your specific beans.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository("ngConfiguration") | |
public class NGConfiguration { | |
@Value("${application.title}") | |
private String applicationTitle; | |
public String getApplicationTitle() { | |
return applicationTitle; | |
} | |
} |
No comments:
Post a Comment