Reputation: 743
I am trying to read properties from application.yml in spring-bean.xml like this:
<bean name="#{bean.name}" />
Is it possible ? or I should specify location of my application.yml file?
Upvotes: 5
Views: 8622
Reputation: 6354
Yes It's Possible
For YAML Properties
You have to use YamlPropertiesFactoryBean
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:application.yml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
Then define your property in src/main/resource/application.yaml
bean:
name: foo
Now use can use the property in xml to create a bean
<bean name="${bean.name}"
class="net.asifhossain.springmvcxml.web.FooBar"/>
Here's my complete XML config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:application.yaml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
<bean name="${bean.name}" class="net.asifhossain.springmvcxml.web.FooBar"/>
</beans>
Upvotes: 8