我一直在研究Hibernate 4和Spring 4.我收到了这个错误并且在过去的4天里一直在努力。 我已经在互联网上尝试了所有这些问题的所有解决方案,但它们都没有奏效。请帮帮我。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.cumulis.dao.impl.TrackDAOImpl] for bean with name 'trackDAO' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: com.cumulis.dao.impl.TrackDAOImpl
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.cumulis.dao.impl.TrackDAOImpl] for bean with name 'trackDAO' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: com.cumulis.dao.impl.TrackDAOImpl
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.cumulis.dao.impl.TrackDAOImpl] for bean with name 'trackDAO' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: com.cumulis.dao.impl.TrackDAOImpl
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1351)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:597)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1444)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:445)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:423)
Caused by: java.lang.ClassNotFoundException: com.cumulis.dao.impl.TrackDAOImpl
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
Related cause:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.cumulis.dao.impl.TrackDAOImpl] for bean with name 'trackDAO' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: com.cumulis.dao.impl.TrackDAOImpl
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1351)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:628)
Caused by: java.lang.ClassNotFoundException: com.cumulis.dao.impl.TrackDAOImpl
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
java.lang.ClassNotFoundException: com.cumulis.dao.impl.TrackDAOImpl
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
java.lang.NullPointerException
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1526)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1440)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:348)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.codejava</groupId>
<artifactId>cumulis</artifactId>
<name>CRUDWebApp</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.8.0.6</java-version>
<org.springframework.version>4.2.2.RELEASE</org.springframework.version>
<org.aspectj-version>1.6.10</org.aspectj-version>
</properties>
<repositories>
<repository>
<id>Java.Net</id>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<dependencies>
<!-- To correct java.lang.ClassNotFoundException: com.sun.faces.config.ConfigureListener error on Tomcat startup -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.12</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- SLF4J seems to be required for Spring Framework -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
<!-- Hibernate ORM framework dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>ejb3-persistence</artifactId>
<version>3.3.2.Beta1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- Our Logging mechanism is based on log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.4.1</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是我的TrackController.java。我知道@Component注释是多余的。
package com.cumulis.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.cumulis.dao.TrackDAO;
import com.cumulis.model.Track;
import com.cumulis.service.TrackService;
@Component
@Controller
public class TrackController {
@Autowired
private TrackDAO trackDao;
@RequestMapping("/")
public ModelAndView handleRequest() throws Exception {
List<Track> trackList = trackDao.getAll();
ModelAndView model = new ModelAndView("trackList");
model.addObject("trackList", trackList);
return model;
}
@RequestMapping(value = "/new", method = RequestMethod.GET)
public ModelAndView newUser() {
ModelAndView model = new ModelAndView("trackForm");
model.addObject("track", new Track());
return model;
}
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView editUser(HttpServletRequest request) {
int trackId = Integer.parseInt(request.getParameter("id"));
Track track = trackDao.get(trackId);
ModelAndView model = new ModelAndView("trackForm");
model.addObject("track", track);
return model;
}
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public ModelAndView deleteUser(HttpServletRequest request) {
int trackId = Integer.parseInt(request.getParameter("id"));
trackDao.delete(trackId);
return new ModelAndView("redirect:/");
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveTrack(@ModelAttribute Track track) {
trackDao.update(track);
return new ModelAndView("redirect:/");
}
}
TrackDAOImpl.java
package com.cumulis.dao.impl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.cumulis.dao.*;
import com.cumulis.model.*;
import org.hibernate.Criteria;
import org.hibernate.Query;
@Component
@Repository
public class TrackDAOImpl implements TrackDAO {
private SessionFactory sessionFactory;
public TrackDAOImpl() {
}
public TrackDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
@Transactional
public ModelAndView add(Track track) {
sessionFactory.getCurrentSession().save(track);
return null;
}
@Override
@Transactional
public Track get(long track_id){
String hql = "from Track where id=" + track_id;
Query query = sessionFactory.getCurrentSession().createQuery(hql);
@SuppressWarnings("unchecked")
List<Track> trackList = (List<Track>) query.list();
if (trackList != null && !trackList.isEmpty()) {
return trackList.get(0);
}
return null;
//return (Track)sessionFactory.getCurrentSession().get(Track.class, track_id);
}
@Override
@Transactional
public void delete(long track_id) {
Track trackToDelete = new Track();
trackToDelete.setId(track_id);
sessionFactory.getCurrentSession().delete(trackToDelete);
//sessionFactory.getCurrentSession().delete(track_id);
}
@Override
@Transactional
public void update(Track track) {
sessionFactory.getCurrentSession().update(track);
}
@Override
@Transactional
public List<Track> getAll() {
@SuppressWarnings("unchecked")
List<Track> trackList = (List<Track>) sessionFactory.getCurrentSession()
.createCriteria(Track.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return trackList;
}
}
TrackDAO.java
package com.cumulis.dao;
import java.util.List;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import com.cumulis.model.Track;
@Component
public interface TrackDAO {
public ModelAndView add(Track track);
public Track get(long track_id);
public void delete(long track_id);
public void update(Track track);
public List<Track> getAll();
}
的hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.cumulis.model.Track" table="track">
<meta attribute="class-description">
This class contains the track highlight and entry point
to other data found in subtrack and trackpoint.
</meta>
<id name="id" type="long" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="created" column="created" type="long"/>
<property name="duration" column="duration" type="double"/>
<property name="distance" column="distance" type="double"/>
<property name="speed" column="speed" type="double"/>
</class>
</hibernate-mapping>
servlet的context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.cumulis" />
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/cumulis"/>
<property name="username" value="root"/>
<property name="password" value="xxxxxx"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.cumulis.controller</value>
<value>com.cumulis.dao</value>
<value>com.cumulis.dao.impl</value>
<value>com.cumulis.model</value>
<value>com.cumulis.service</value>
<value>com.cumulis.service.impl</value>
</list>
</property>
<property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="trackDAO" class="com.cumulis.dao.impl.TrackDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
</beans>
答案 0 :(得分:0)
从以下类中删除@Component:
TrackDAO,TrackDAOImpl,TrackController
@Component是多余的,因为你已经在使用@ Repository,@ Controller等。
确保您还自动扫描班级所在的文件夹
XML和Java Config之间也有一个奇怪的组合。选择一个并坚持下去,没有必要同时运行。
编辑1: 如请求链接到示例,显示如何手动声明&amp;自动扫描XML和Java Config。
http://www.mkyong.com/spring/spring-auto-scanning-components/
答案 1 :(得分:0)
以下更改有效 @Controller 公共类TrackController {
@Autowired
private TrackDAO trackDao;
@RequestMapping("/")
public ModelAndView handleRequest() throws Exception {
List<Track> trackList = trackDao.getAll();
ModelAndView model = new ModelAndView("trackList");
model.addObject("trackList", trackList);
return model;
}
...
和
@Repository("TrackDAOImpl")
public class TrackDAOImpl implements TrackDAO {
@Autowired
private SessionFactory sessionFactory;
@Autowired
public TrackDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
@Transactional
public List<Track> getAll() {
@SuppressWarnings("unchecked")
List<Track> trackList = (List<Track>) sessionFactory.getCurrentSession()
.createCriteria(Track.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return trackList;
}
...
我还必须删除多个hibernate-annotation jar。我还必须在servlet-context.xml中更改它。
<bean id="TrackDAOImpl" class="com.cumulis.dao.TrackDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>