从ServletContext资源解析XML文档时出现意外异常

时间:2013-10-14 02:48:36

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

我找到了一个基本的Spring安全示例,但我通常无法在tomcat上运行它。 我知道这个错误消息可能是由配置xml文件引起的,但我找不到它。 谢谢你的帮助:)

1. Error Message

java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
        at java.lang.Class.getDeclaredMethods(Class.java:1845)
        at org.springframework.core.type.StandardAnnotationMetadata.hasAnnotated
Methods(StandardAnnotationMetadata.java:136)
        at org.springframework.context.annotation.ConfigurationClassBeanDefiniti
onReader.checkConfigurationClassCandidate(ConfigurationClassBeanDefinitionReader
.java:318)

2. File Structure

enter image description here

3. web.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/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>Spring MVC Application</display-name>

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

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/mvc-dispatcher-servlet.xml,
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>

    <!-- Spring Security -->
    <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>

4. mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.mkyong.common.controller" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>mymessages</value>
            </list>
        </property>
    </bean>

</beans>

5.spring-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">
        <intercept-url pattern="/welcome*" access="ROLE_USER" />

        <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/loginfailed"
             />
    </http>

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

</beans:beans>

3 个答案:

答案 0 :(得分:3)

这是stacktrace

的一部分
Constructor threw exception; nested exception is java.lang.NoC
lassDefFoundError: org/springframework/aop/config/AbstractInterceptorDrivenBeanD
efinitionDecorator

表示Aspect Oriented Programming (AOP) Framework中不存在classpath个罐子。因此,您需要将spring-aop(spring-aop-3.0.5.RELEASE.jar)罐子添加到classpath

答案 1 :(得分:1)

您需要将spring.jar添加到您的库中。弹簧安全罐会引用它。因此初始化失败。

答案 2 :(得分:1)

我在我的项目中发现了同样的异常,但在添加了spring-aop-4.1.6.RELEASE.jar之后,我解决了这个问题。请注意你要在项目中添加的jar版本...... :)