Mạnh Đức
Mạnh Đức

Reputation: 1

Error while learning spring

I'm begining to learn Spring and getting an error while doing my first project.

I'm getting the error:

org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource

Here is my web.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>

spring-servlet.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/>

my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>test</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.0.RELEASE</version>
    </dependency>
</dependencies>

I have google a lot and tried some answer (added spring-aop, etc...) but they didn't work. I have no idea what I have to do to fix this.

Upvotes: 0

Views: 183

Answers (4)

lokesh kotha
lokesh kotha

Reputation: 92

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

Please check by changing this in web.xml

Upvotes: 0

Jorge L. Morla
Jorge L. Morla

Reputation: 630

When a library or class is present in compilation-time but not in run-time cause a NoClassDefFoundError. Witch IDE are you using? check in target directory (source compiled) if exits a lib sub directory and contain spring-oap dependency, if you are using intellij idea check if the dependency are added in the war file.

relative path

target/your-project/WEB-INF/lib

check if contains dependencies

Upvotes: 1

jrmullen
jrmullen

Reputation: 346

After searching for nested exception is java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource I found Got java.lang.NoClassDefFoundError while learning Spring framework

Try adding

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.2.1.RELEASE</version>
</dependency>

to your pom.xml

Upvotes: 0

Jens
Jens

Reputation: 69440

As the error message java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource told you, you miss spring-aop in your classpath. Add it as a dependency:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version><your-spring-version></version>
</dependency>

Upvotes: 1

Related Questions