0

The Problem is very weird.My IDE is IDEA. Spring version is 4.3.1.RELEASE.The Blow is my project structure enter image description here

The Blow is web.xml(Simple)

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

    <display-name>Spring MVC Application</display-name>
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,
            /WEB-INF/infrastructure.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/home/profile</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

The apllicationContext.xml just one line

<context:component-scan base-package="net.codespace.entity, net.codespace.DAO, net.codespace.service" />

The Blow is SpringMVC-servlet.xml

<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-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="net.codespace" />
    <mvc:annotation-driven />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/home/" />
        <property name="suffix" value=""/>
    </bean>
</beans>

The PersonDAO is interface.The Code is Blow

import net.codespace.entity.Person;

public interface PersonDAO {
    public void save(Person p);
    public void findAll();
}

PersonDAOImpl is implementation.

import net.codespace.entity.Person;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("personDAO")
public class PersonDAOImpl implements PersonDAO{
    @Autowired
    private SessionFactory sessionFactory;
    public void save(Person p){

    }
    public void findAll(){

    }
}

Same,The PersonService is interface,

package net.codespace.service;

/**
 * Created by mark on 2016/7/26.
 */
import net.codespace.entity.Person;

public interface PersonService {
    public void savePerson(Person p);
    public void getAllPerson(Person p);
}

PersonServiceImpl is blow

import net.codespace.entity.Person;
import net.codespace.DAO.PersonDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("personService")
@Transactional
public class  PersonServiceImpl implements PersonService{
    @Autowired
    private PersonDAO personDAO;

    public void savePerson(Person p){

    }
    public void getAllPerson(Person p){
        System.out.println("punk your pieese");
    }
}

The pom

<?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>net.markliang</groupId>
    <artifactId>lsc-toy</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
            <jackson.version>2.6.3</jackson.version>
            <hibernate.version>4.3.5.Final</hibernate.version>
            <spring.version>4.3.1.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

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

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

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

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

        <dependency>
            <groupId>tomcat</groupId>
            <artifactId>servlet-api</artifactId>
            <version>5.5.23</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.3</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.3.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.31</version>
        </dependency>
    </dependencies>

</project>

When I deploy this Project, I got these Error(Show Important)

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService' defined in file 
[C:\Users\mark\IdeaProjects\SMS01\out\artifacts\SMS01_war_exploded\WEB-INF\classes\net\codespace\service\PersonServiceImpl.class]: Post-processing failed of bean type [class net.codespace.service.PersonServiceImpl] failed; nested exception is java.lang.IllegalStateException: Failed to introspect bean class [net.codespace.service.PersonServiceImpl] for persistence metadata: could not find class that it depends on
Caused by: java.lang.IllegalStateException: Failed to introspect bean class [net.codespace.service.PersonServiceImpl] for 
persistence metadata: could not find class that it depends on
/* some info */

Caused by: java.lang.NoClassDefFoundError: Lnet/codespace/DAO/PersonDAO;
/* some info */

Caused by: java.lang.ClassNotFoundException: net.codespace.DAO.PersonDAO
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1891)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1734)
  /*some info */

Could someone help me? If you need more detail info, I can Send u a mail.Thanks

markliang
  • 41
  • 1
  • 7

2 Answers2

1

You need to know the difference between an applicationContext and a servlet-context (have a look at this answer.

Your servlet context shouldn't be scanning the persistence layer, because the persistence layer should have an application scope , which means to be available for all the servlets.

In your SpringMvc, scan only controllers:

<context:component-scan base-package="net.codespace.contrller" />

In your application Context you should scan repositories and services so you can bean wiring them.

Community
  • 1
  • 1
Rafik BELDI
  • 4,140
  • 4
  • 24
  • 38
0

The java.lang.ClassNotFoundException: net.codespace.DAO.PersonDAO and java.lang.NoClassDefFoundError: Lnet/codespace/DAO/PersonDAO should be the main concern.

You need to check why PersonDAO is not available on the classpath. Try the following:

  • Does the package declaration in your PersonDAO.java file match the folder structure? (it's either omitted in your post or missing from the file) i.e. package net.codespace.DAO;
  • Run mvn clean install and (making sure that it compiles and finishes ok) check that target/classes/net/codespace/DAO/PersonDAO.class exists.
  • If the above is ok then you will need to examine the classpath to see why it cannot find this class - this will depend on how you are running the application (in the IDE or Maven etc).

NB This shouldn't matter but Java naming convention for packages is lower case so ideally your package name should be net.codespace.dao

tchambers
  • 456
  • 3
  • 5