我正在使用Spring 3.2.3.RELEASE + Hibernate 4.3.5.Final而且我无法启动我的应用程序(在tomcat 6上)
错误:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.boot.registry.classloading.spi.ClassLoaderService]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1276)
at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:625)
at org.apache.catalina.manager.HTMLManagerServlet.doGet(HTMLManagerServlet.java:136)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:194)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:563)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.boot.registry.classloading.spi.ClassLoaderService]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:125)
at org.hibernate.envers.event.spi.EnversIntegrator.integrate(EnversIntegrator.java:76)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:307)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1797)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1868)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
... 35 more
applicationContext:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/springapp" />
<property name="username" value="hibernate" />
<property name="password" value="hib" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
一个实体:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import springapp.service.NewRecipe;
import com.google.common.base.Strings;
@Entity
@Table
public class Recipe {
public static final String D_ID = "id";
public static final String D_NAME = "name";
public static final String D_DESCRIPTION = "description";
@Id @GeneratedValue @Column(name = D_ID) private long id;
@Column(nullable = false, unique = true, name = D_NAME) private String name;
@Column(nullable = false, name = D_DESCRIPTION) private String description;
@ManyToOne(optional = false) private Category category;
public Recipe() {
// for ORM
}
}
一个人:
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import springapp.domain.Recipe;
import springapp.service.NewRecipe;
@Repository("recipeDao")
public class HibernateRecipeDao implements RecipeDao {
@Autowired private SessionFactory sessionFactory;
@Override
public List<Recipe> getRecipes() {
final Session session = sessionFactory.getCurrentSession();
final Criteria criteria = session.createCriteria(Recipe.class).addOrder(Property.forName(Recipe.D_ID).asc());
final List<Recipe> recipes = criteria.list();
return recipes;
}
}
一位经理:
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springapp.domain.Recipe;
import springapp.repository.RecipeDao;
@Service("recipeManager")
public class HibernateRecipeManager implements RecipeManager {
@Autowired private RecipeDao recipeDao;
@Transactional
@Override
public List<Recipe> getRecipes() {
return recipeDao.getRecipes();
}
}
答案 0 :(得分:2)
好的,我解决了这个问题。
首先,Spring 3.2 不会使用Hibernate 4.3。您需要将Spring升级到版本4或将降级 Hibernate升级到版本4.2( 4.2.11.Final 对我来说还可以。)
你们中的第二个人不能在applicationContext中拥有&lt; tx:annotation-driven /&gt; 。您需要在调度程序上下文中使用它(&lt; servlet_name&gt; -servlet.xml)。我不太清楚为什么,但在这里你可以参考:forum.spring.io
从您的堆栈跟踪和代码判断,您使用组件扫描并具有重复的组件。如何克服这一点在参考指南中解释并在论坛上多次回答
噢!太棒了!只需移动&lt; tx:annotation-driven /&gt;从applicationContext到dispatcherContext,它现在正在工作!感谢&#39;!小号