2013-10-14 90 views
2

我找到了一个基本的spring安全示例,但是我不能在tomcat上运行它。 我知道这个错误信息可能是由配置xml文件引起的,但是我找不到它。 感谢您的帮助:)意外的异常从ServletContext资源解析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

这是你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

+0

我添加'spring-aop'后,仍然有一些错误信息。 – LoveTW

+0

你在使用eclipse吗? –

+0

是的!我发现我失去了'org.springframework.transaction-3.0.5.RELEASE'。感谢您的帮助! – LoveTW

1

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

+0

是org.springframework.core.jar不是spring.jar? – LoveTW

+1

打开并查看它是否具有此类... org/springframework/aop/config/AbstractInterceptorDrivenBeanDefinitionDecorator。我想你也需要spring-aop.jar –

+0

你是对的!但我仍然有一些错误信息。我编辑了我的帖子。谢谢:) – LoveTW

1

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

相关问题