java - 如何从不同的maven项目导入spring资源?

标签 java xml spring maven spring-mvc

我正在尝试使用一个带有客户端的 Maven 项目和另一个带有安全性的 Maven 项目来构建 Web 应用程序。我的 Maven 依赖项是正确的,但我无法在安全模块中加载资源。

这是我的 web.xml (src/main/webapp/WEB-INF/web.xml):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            /WEB-INF/spring/appServlet/servlet-context.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <mime-mapping>
        <extension>js</extension>
        <mime-type>application/x-javascript</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>properties</extension>
        <mime-type>application/text</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xml</extension>
        <mime-type>application/xml</mime-type>
    </mime-mapping>

    <!-- Enables Security -->
    <!-- <listener> <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> 
        </listener> <filter> <filter-name>monitoring</filter-name> <filter-class>net.bull.javamelody.MonitoringFilter</filter-class> 
        </filter> <filter-mapping> <filter-name>monitoring</filter-name> <url-pattern>/*</url-pattern> 
        </filter-mapping> <filter> <filter-name>springSecurityFilterChain</filter-name> 
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
        </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> 
        <url-pattern>/*</url-pattern> </filter-mapping> -->


</web-app>

在我的 root-context.xml 中,我尝试从另一个 Maven 模块导入另一个资源:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <beans:bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <beans:property name="location" value="classpath:resources.properties"> </beans:property> 
    </beans:bean> -->
<beans:import
    resource="classpath:/META-INF/spring/appServlet/servlet-security.xml" />
</beans:beans>

我的 servlet-security.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">
 
    <http auto-config="true"  use-expressions="true">
        <intercept-url pattern="/login" access="permitAll" />
        <intercept-url pattern="/logout" access="permitAll" />
        <intercept-url pattern="/accessdenied" access="permitAll" />
        <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
        <form-login login-page="/login" default-target-url="/list" authentication-failure-url="/accessdenied" />
        <logout logout-success-url="/logout" />
    </http>
 
    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="vitorn" password="password1" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

当我在服务器中部署我的 war 时,我得到了这个异常。我如何从另一个模块导入资源???

INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [META-INF/spring/appServlet/servlet-security.xml]
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/META-INF/spring/appServlet/servlet-security.xml]
Offending resource: ServletContext resource [/WEB-INF/spring/root-context.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/spring/appServlet/servlet-security.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/spring/appServlet/servlet-security.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)

最佳答案

使用 META-INF 作为 spring 文件位置不是一个好习惯。我认为它不是类路径的一部分。在Maven项目中,一般是添加在resources文件夹下。

例如src/main/resources/spring/

移动你的文件并尝试使用以下配置

<beans:import
    resource="classpath:/spring/appServlet/servlet-security.xml" />

关于java - 如何从不同的maven项目导入spring资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19065975/

相关文章:

java - Hibernate Spring 安全修改设计如何

xml - 解码包含数据和子节点的 xml 节点

c# - XSLT,将所有负节点和所有节点求和为绝对值

java - 向打开浏览器 URL 的 Activity 添加按钮

java - Spring Data JPA 数据复制

spring - Pentaho j_spring_security_check 混合内容。 HTTPS 到 HTTP

spring - 如何在 Spring MongoDB 中将 2 个整数值连接成一个字符串

java - 为什么需要单击两次才能突出显示我的 JTree?

java - 方面未执行

c# - 如何确定数学表达式的求值顺序?