您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

从xml配置读取spring yml属性

从xml配置读取spring yml属性

是的,有可能

对于YAML属性

您必须使用

<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesfactorybean">
<property name="resources" value="classpath:application.yml"/>

然后在src / main / resource / application.yaml中定义您的属性

bean:

name: foo

现在使用可以使用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/@R_403_875@s-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>
Java 2022/1/1 18:24:53 有611人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶