从xml配置中读取spring yml属性
我试图从spring-bean.xml中的application.yml读取属性,如下所示:从xml配置中读取spring yml属性,spring,spring-boot,Spring,Spring Boot,我试图从spring-bean.xml中的application.yml读取属性,如下所示: <bean name="#{bean.name}" /> 可能吗?或者我应该指定application.yml文件的位置?是的,这是可能的 对于YAML属性 您必须使用YAMLProperties FactoryBean <bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPr
<bean name="#{bean.name}" />
可能吗?或者我应该指定application.yml文件的位置?是的,这是可能的
对于YAML属性
<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="${bean.name}"
class="net.asifhossain.springmvcxml.web.FooBar"/>
<?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 Boot加载这些文件。使用${bean.name}
代替{bean.name}
。后者是一个SpEL表达式。感谢您的回答,但我希望从YAML读取值,而不是从*.properties读取值。
<?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>