我开始学习Spring并在做第一个项目时遇到错误。
我收到了错误:
org.springframework.beans.factory.BeanDefinitionStoreException:从ServletContext资源[/WEB-INF/spring-servlet.xml]解析XML文档时出现意外异常;嵌套异常是java.lang.NoClassDefFoundError:org / springframework / aop / TargetSource
这是我的web.xml:
protected void BulkInsertFileRecords(int requestId) {
BulkInsert(requestId);
}
弹簧servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
我的pom.xml:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
<context:component-scan base-package="controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
我已经谷歌了很多,并尝试了一些答案(添加spring-aop等...)但他们没有工作。我不知道我要做些什么来解决这个问题。
答案 0 :(得分:1)
正如错误消息java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
告诉您的那样,您错过了类路径中的spring-aop
。将其添加为依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version><your-spring-version></version>
</dependency>
答案 1 :(得分:1)
当编译时存在库或类但在运行时没有时会导致NoClassDefFoundError。你在使用Witch IDE吗?检查目标目录(源代码编译)如果退出lib子目录并包含spring-oap依赖项,如果你正在使用intellij idea检查是否在war文件中添加了依赖项。
相对路径
target/your-project/WEB-INF/lib
检查是否包含依赖项
答案 2 :(得分:0)
寻找之后
nested exception is java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
我找到了Got java.lang.NoClassDefFoundError while learning Spring framework
尝试添加
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
到你的pom.xml
答案 3 :(得分:0)
<servlet>
<servlet-name>spring</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
</servlet>
请在web.xml中更改此内容