问题非常奇怪。我的IDE是IDEA。 Spring版本是4.3.1.RELEASE.Blow是我的项目结构
Blow是web.xml(简单)
<?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>
apllicationContext.xml只有一行
<context:component-scan base-package="net.codespace.entity, net.codespace.DAO, net.codespace.service" />
Blow是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>
PersonDAO是界面。代码是Blow
import net.codespace.entity.Person;
public interface PersonDAO {
public void save(Person p);
public void findAll();
}
PersonDAOImpl是实现。
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(){
}
}
同样,PersonService是接口,
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正在打击
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");
}
}
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>
当我部署此项目时,我收到了这些错误(显示重要)
严重: 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 */
有人可以帮助我吗?如果您需要更多详细信息,我可以发送邮件。谢谢
答案 0 :(得分:1)
您需要了解applicationContext
和servlet-context
之间的区别(请查看此answer。
您的servlet上下文不应该扫描持久层,因为持久层应该具有应用程序范围,这意味着可用于所有servlet。
在SpringMvc中,仅扫描控制器:
<context:component-scan base-package="net.codespace.contrller" />
在您的应用程序上下文中,您应该扫描存储库和服务,以便可以对它们进行bean连接。
答案 1 :(得分:0)
java.lang.ClassNotFoundException: net.codespace.DAO.PersonDAO
和java.lang.NoClassDefFoundError: Lnet/codespace/DAO/PersonDAO
应该是主要问题。
您需要检查为什么PersonDAO
在类路径上不可用。请尝试以下方法:
package net.codespace.DAO;
mvn clean install
并(确保编译并完成正常)检查target/classes/net/codespace/DAO/PersonDAO.class
是否存在。注意这不是问题,但Java naming convention for packages是小写的,所以理想情况下你的包名应该是net.codespace.dao