根据spring配置文件导入bean资源

时间:2019-07-05 18:18:52

标签: java xml spring spring-boot spring-mvc

下面是我的spring项目中servlet-context.xml的代码。如果配置文件是prod,我想导入demo.xml文件,如果配置文件是test,我想导入demo-test.xml。

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:task="http://www.springframework.org/schema/task"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <beans profile="test">
        <import resource="demo-test.xml"></import>
    </beans>
    <beans profile="prod">
        <import resource="demo.xml"></import>
    </beans>


    <task:annotation-driven
            executor="defaultExecutor"/>
    <!-- add an exception handler here. -->

    <task:executor id="defaultExecutor"
                   pool-size="50-200"
                   queue-capacity="1200"
                   keep-alive="10"
                   rejection-policy="CALLER_RUNS"/>

</beans:beans>

但是我在第The matching wildcard is strict, but no declaration can be found for element 'beans'.行出现了错误<beans profile="test">,在'resource' attribute should be defined上出现了<import resource="demo-test.xml"></import>错误

基于error in spring xml config relating to bean profiles,我什至尝试将上下文更改为“ http://www.springframework.org/schema/beans/spring-beans-3.2.xsd”,但是没有运气。有人可以帮助我了解此错误的含义以及如何解决此问题吗?

1 个答案:

答案 0 :(得分:0)

xml文件顶部的<beans:beans xmlns="http://www.springframework.org/schema/mvc"使用mvc而不是beans,这意味着默认名称空间是mvc而不是beans。要么限定bean标记:使用<beans:beans profile="test">...代替<beans profile="test">...,使用<beans:import resource...代替<import resource...,或者将默认名称空间更改为beans,以将xml用作是