【问题标题】:Spring MVC : adding securityConfig.xml along with dispatcherContext.xmlSpring MVC:添加 securityConfig.xml 和 dispatcherContext.xml
【发布时间】:2014-06-15 17:35:08
【问题描述】:

我在 web.xml 中遇到此代码,但我不明白如何将 securityConfig.xml 和 dispatcherContext.xml 添加到类路径中

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:dispatcherContext.xml</param-value>
</context-param>

谁能给我一些建议

更新

当我在dispatcherContext.xml 中包含&lt;import resource="classpath:securityConfig.xml" /&gt; 时,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/util
            http://www.springframework.org/schema/util/spring-util.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <import resource="classpath:securityConfig.xml" />

我收到以下异常

ContextLoader - Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:securityConfig.xml]
Offending resource: class path resource [dispatcherContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [securityConfig.xml]; nested exception is java.io.FileNotFoundException: class path resource [securityConfig.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)

【问题讨论】:

    标签: java spring spring-security web.xml


    【解决方案1】:

    只需在根上下文定义(在 yr 情况下为 dispatcherContext.xml)文件中使用导入:

    <!-- Load spring security related configuration -->
    <import resource="classpath:securityConfig.xml"/>
    

    但是您可以通过在文件名之间放置一个空格来提供多个:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>dispatcherContext.xml securityConfig.xml</param-value>
    </context-param>
    

    【讨论】:

    • 但是类路径呢,classpath: 在&lt;param-value&gt;dispatcherContext.xml securityConfig.xml&lt;/param-value&gt; 中丢失了
    • 但出现异常 --> org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/securityConfig.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/securityConfig.xml]
    • 我已将securityConfig.xml 放在与dispatcherContext.xml 相同的位置,即资源文件夹中
    • 如果我删除dispatcherContext.xml,dispatcherContext.xml 正在恢复
    • 传统的方法是从 xml 文件中引用其他文件,或者如果执行基于 java 的 conf,则引用 java 类。 web.xml 中的多个上下文定义意味着不同的应用程序,但这实际上并不是您想要的。
    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 2012-06-08
    • 1970-01-01
    • 2012-10-07
    • 2014-12-31
    • 2012-08-05
    • 2015-04-16
    • 1970-01-01
    相关资源
    最近更新 更多