我正在尝试从 spring-bean.xml 中的 application.yml 读取属性,如下所示:
<bean name="#{bean.name}" />
是否可以 ?或者我应该指定我的 application.yml 文件的位置?
最佳答案
是的,这是可能的
For YAML Properties
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:application.yml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
bean:
name: foo
<bean name="${bean.name}"
class="net.asifhossain.springmvcxml.web.FooBar"/>
这是我完整的 XML 配置
<?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>
关于spring - 从 xml 配置中读取 spring yml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46094792/