从 xml 配置中读取 spring yml 属性 [英] Read spring yml properties from xml configuration
本文介绍了从 xml 配置中读取 spring yml 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从 spring-bean.xml 中的 application.yml 中读取属性,如下所示:
I am trying to read properties from application.yml in spring-bean.xml like this:
<bean name="#{bean.name}" />
有可能吗?或者我应该指定我的 application.yml 文件的位置?
Is it possible ? or I should specify location of my application.yml file?
推荐答案
Yes It's possible
Yes It's Possible
对于 YAML 属性
你必须使用 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"/>
然后在 src/main/resource/application.yaml 中定义你的属性
Then define your property in src/main/resource/application.yaml
bean:
name: foo
现在使用可以使用xml中的属性创建bean
Now use can use the property in xml to create a bean
<bean name="${bean.name}"
class="net.asifhossain.springmvcxml.web.FooBar"/>
这是我的完整 XML 配置
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>
这篇关于从 xml 配置中读取 spring yml 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文