【问题标题】:import bean resources based on spring profiles基于spring配置文件导入bean资源
【发布时间】:2019-07-05 18:18:52
【问题描述】:

下面是我的 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>

但是对于&lt;beans profile="test"&gt; 行和'resource' attribute should be defined 对于&lt;import resource="demo-test.xml"&gt;&lt;/import&gt; 行我收到错误The matching wildcard is strict, but no declaration can be found for element 'beans'.

基于error in spring xml config relating to bean profiles,我什至尝试将上下文更改为“http://www.springframework.org/schema/beans/spring-beans-3.2.xsd”,但没有成功。有人能帮我理解这个错误是什么意思吗?我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    xml 文件顶部的&lt;beans:beans xmlns="http://www.springframework.org/schema/mvc" 使用mvc 而不是beans,这意味着默认命名空间是mvc 而不是beans。或者,限定 bean 标签:使用&lt;beans:beans profile="test"&gt;... 代替&lt;beans profile="test"&gt;...&lt;beans:import resource... 代替&lt;import resource...,或者将默认命名空间更改为beans 以按原样使用xml

    【讨论】:

    • 不,我已经在 @ImportResources 的 Application.java 文件中导入了这个 servlet-context.xml
    • 而且错误是别的,你可以在问题的最后检查。
    • 查看此问题的正确答案:stackoverflow.com/questions/26836345/…。 xml 文件顶部的&lt;beans:beans xmlns="http://www.springframework.org/schema/mvc" 使用mvc 而不是beans,否则,限定bean 标签:使用&lt;beans:beans profile="test"&gt;... 而不是&lt;beans profile="test"&gt;...。尝试更改并再次测试
    猜你喜欢
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    相关资源
    最近更新 更多