I have in my applicationContext.xml
<context:property-placeholder location="classpath*:*.properties" /> <bean id="clientPreferencesManager" class="pl.bildpresse.bildchat2.business.ClientPreferencesManager" > <property name="clientApiUrl" value="${clientapi.url}" /> </bean>
Is it possible to do the same by autowire ? Something like :
@Autowired @Qualifier("${clientapi.url}") public void setClientApiUrl(String clientApiUrl) { this.clientApiUrl = clientApiUrl; }
The context:property-placeholder tag is used to externalize properties in a separate file. It automatically configures PropertyPlaceholderConfigurer , which replaces the ${} placeholders, which are resolved against a specified properties file (as a Spring resource location).
Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.
Parse properties file with @ConfigurationProperties in a Spring boot. @ConfigurationProperties in spring boot is another way to read properties files. Add setters and getters for a member variable. Object of this class holds the content of properties with values.
You can use @Value
:
@Value("${clientapi.url}") public void setClientApiUrl(String clientApiUrl) { this.clientApiUrl = clientApiUrl; }
It took me some time to understand why it didn't work. I always used a #
instead of a $
. I always got the message:
EL1008E:(pos 0): Field or property 'secretkey' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
Just had to change it from:
@Value("#{secretkey}')
to
@Value('${secretkey}')
I hope this saves somebody's time.
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