0

我试图让 Spring 和 Hibernate 4 一起工作,但出现了这些错误:

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
Mar 15, 2013 11:00:54 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: 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 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.ExceptionInInitializerError
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1444)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:518)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ExceptionInInitializerError
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:244)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1503)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1441)
    ... 22 more
Caused by: java.lang.IllegalStateException: Expected method not found: java.lang.NoSuchMethodException: org.hibernate.cfg.Configuration.addAnnotatedClass(java.lang.Class)
    at org.springframework.util.ClassUtils.getMethod(ClassUtils.java:626)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.<clinit>(LocalSessionFactoryBuilder.java:68)
    ... 25 more

这是我的道课:

@Repository
@Transactional
@Component
public class UtilisateurDAO extends HibernateDaoSupport{

@Autowired
private SessionFactory sessionFactory;

public Utilisateur getById(int id)
{
    return (Utilisateur) sessionFactory.getCurrentSession().get(Utilisateur.class, id);
}

这是我的控制器:

   @Controller
   public class UtilisateurController
   {
@Autowired
private UtilisateurDAO utilisateurDAO;

@RequestMapping("/searchUtilisateurs")
public ModelAndView searchContacts(@RequestParam(required= false, defaultValue="") String name)
{
    ModelAndView mav = new ModelAndView("showUtilisateurs");
    List<Utilisateur> utilisateurs = utilisateurDAO.searchUtilisateur(name.trim());
    mav.addObject("SEARCH_UTILISATEURS_RESULTS", utilisateurs);
    return mav;
}

最后,这是我的 applicationContext.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:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:annotation-config />

    <bean id="datasource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/JEEFRAMEWORK" />
        <property name="username" value="postgres" />
        <property name="password" value="admin" />
    </bean>

    <!--  Déclaration Session Factory -->

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="datasource" />
        <property name="annotatedClasses">
           <list>
               <value>model.Utilisateur</value>    
           </list>
        </property>
        <property name="packagesToScan" value="dao" />
        <property name="configLocation" value="classpath:/hibernate.cfg.xml"/>
    </bean>


    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="UtilisateurDAO" class="dao.UtilisateurDAO">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

有人可以帮我解决这个错误吗?

提前致谢 !

4

1 回答 1

0

在 Hibernate 3 中,Configuration 没有方法 addAnnotatedClass(Class annotatedClass),它只在 Hibernate 4 中。所以你的类路径中可能有 Hibernate 3 Configuration 类。

来自 LocalSessionFactoryBuilder 的参考 IllegalStateException

于 2013-03-15T22:17:06.563 回答