gpt4 book ai didi

java - SpringMVC BeanCreationException : creating bean with name 'personService' defined in file PersonServiceImpl. 类?

转载 作者:行者123 更新时间:2023-12-01 09:38:51 26 4
gpt4 key购买 nike

问题很奇怪。我的IDE是IDEA。 Spring版本是4.3.1.RELEASE。Blow是我的项目结构 enter image description here

打击是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>

applicationContext.xml 只有一行

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

打击的是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 是接口(interface)。代码很简单

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是接口(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 失败了

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

有人可以帮助我吗?如果您需要更多详细信息,我可以给您发送邮件。谢谢

最佳答案

您需要了解 applicationContextservlet-context 之间的区别(看看这个 answer

您的 servlet 上下文不应该扫描持久层,因为持久层应该有一个应用程序范围,这意味着可用于所有 servlet。

在 SpringMvc 中,仅扫描 Controller :

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

在您的应用程序上下文中,您应该扫描存储库和服务,以便您可以对它们进行连接。

关于java - SpringMVC BeanCreationException : creating bean with name 'personService' defined in file PersonServiceImpl. 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38614176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com