Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从xml配置中读取spring yml属性_Spring_Spring Boot - Fatal编程技术网

从xml配置中读取spring 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

我试图从spring-bean.xml中的application.yml读取属性,如下所示:

<bean name="#{bean.name}" />

可能吗?或者我应该指定application.yml文件的位置?

是的,这是可能的

对于YAML属性

  • 您必须使用YAMLProperties FactoryBean

    <bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
        <property name="resources" value="classpath:application.yml"/>
    </bean>
    
    <context:property-placeholder properties-ref="yamlProperties"/>
    
  • 现在use可以使用xml中的属性来创建bean

    <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 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>