java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener添加spring-security

时间:2014-04-12 10:57:07

标签: java spring spring-mvc spring-security

我收到了错误

  

java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener

仅当我在" Spring Security"之间包含以下代码时才进行部署和#34;结束Spring Security"在文件web.xml中。如果我删除XML中的这部分代码,一切正常。

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


 <!-- Spring MVC -->
 <servlet>
 <servlet-name>dispatch</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>
 <servlet-mapping>
 <servlet-name>dispatch</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

 <servlet>
 <servlet-name>floorOperationWS</servlet-name>
 <servlet- class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
 </servlet>
 <servlet-mapping>
<servlet-name>floorOperationWS</servlet-name>
<url-pattern>/floorOperationWS/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>updatefloorServlet</servlet-name>
    <servlet-class>com.services.endpoint.UpdatefloorEventServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>updatefloorServlet</servlet-name>
    <url-pattern>/updatefloorWS</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

    <!-- Spring Security -->

     <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>

<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>

    <!-- END Spring Security -->

</web-app>

- spring-security.xml:

   <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.2.xsd">

<http auto-config="true">
    <intercept-url pattern="/admin**" access="ROLE_USER" />
</http>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="mkyong" password="123456" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

- MAVEN依赖项:

            <properties>        
    <java-version>1.7</java-version>
    <!--  org.springframework-version>3.0.5.RELEASE</org.springframework-v version-->
    <org.springframework-version>4.0.3.RELEASE</org.springframework-version>
    <spring.security.version>3.2.3.RELEASE</spring.security.version>
    <org.aspectj-version>1.6.10</org.aspectj-version>
    <org.slf4j-version>1.5.10</org.slf4j-version>
    <junit.version>4.11</junit.version>
</properties>
<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>
    <!-- Spring 3 dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

<!-- DB connection -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

造成此错误的原因是什么?如何解决?

3 个答案:

答案 0 :(得分:0)

导致此错误的常见问题错误地包含在spring jar中 你没有提到你的ide,所以我假设你正在使用eclipse 解决方法是

1.双击用于部署的服务器

2.单击打开启动配置 - 类路径选项卡

3.单击“添加外部罐子”

4.add spring jar

5.运行你的申请

答案 1 :(得分:0)

我创建了一个示例项目,详细说明了我遇到的两个问题:
&GT; Spring安全版:3.2.3.RELEASE
将它改为3.1.6.RELEASE //(虽然这种情况不应该给出错误)
&GT;在给定的pom你没有添加spring-mvc jar
添加它之后,它适用于我

答案 2 :(得分:0)

改变

 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
    /WEB-INF/spring-security.xml
</param-value>

for in applicationContext.xml

<import resource="spring-security.xml" />

解决了问题