在applicationContext中为sessionFactory创建bean

时间:2013-01-08 16:40:58

标签: java spring hibernate javabeans

更新:显示完整的应用程序上下文

美好的一天。 我尝试集成spring和hibernate以及tapestry,但是在application-context.xml中遇到了一些bean的问题 这里我的pom.xml部分有hibernate和spring。 我使用IDE:Idea 12,支持spring,tapestry和hibernate框架,所以lib目录下有一些jar文件。

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>2.0.6</version>
        </dependency>


      <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.2.Final</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

这是我的applicationcontext.xml,是sessionFactry bean的一部分

   <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/reframework</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>root</value>
        </property>
    </bean>

    <bean id="namingStrategy"
          class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
        <property name="staticField">
            <value>org.hibernate.cfg.ImprovedNamingStrategy.INSTANCE</value>
        </property>
    </bean>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="namingStrategy">
            <ref bean="namingStrategy"/>
        </property>
        <property name="mappingResources">
            <list>
                <!--<value>genericdaotest/domain/Person.hbm.xml</value>-->
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLMyISAMDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>

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

    <bean id="extendedFinderNamingStrategy"
          class="com.packtpub.domain.finder.classes.ExtendedFinderNamingStrategy" />


    <!--  Dao Layer generic config-->
    <bean id="finderIntroductionAdvisor" class="com.packtpub.domain.finder.classes.FinderIntroductionAdvisor"/>
    <bean id="abstractDaoTarget"
          class="com.packtpub.domain.dao.ReDAO" abstract="true">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
        <property name="namingStrategy">
            <ref bean="extendedFinderNamingStrategy" />
        </property>
    </bean>
    <bean id="abstractDao"
          class="org.springframework.aop.framework.ProxyFactoryBean" abstract="true">
        <property name="interceptorNames">
            <list>
                <value>finderIntroductionAdvisor</value>
            </list>
        </property>
    </bean>

    <!--  Dao Layer instances -->
    <bean id="objectDao" parent="abstractDao">
        <property name="proxyInterfaces">
            <value>com.packtpub.domain.dao.objects.IObjectDAO</value>
        </property>
        <property name="target">
            <bean parent="abstractDaoTarget">
                <constructor-arg>
                    <value>com.packtpub.domain.entities.ObjectEntity</value>
                </constructor-arg>
            </bean>
        </property>
    </bean>
</beans>

但是,当我开始我的应用程序时,它给了我一个例外

Error creating bean with name 'sessionFactory' defined in class path resource [WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider

正如我在hibernate 4中搜索后所理解的那样,没有这样的类,但我需要做什么来解决它?

这里是完整的堆栈跟踪

[DEBUG] AppModule.IObjectService Loading class com.packtpub.services.ObjectService.
[DEBUG] AppModule.IObjectService Marking class com.packtpub.services.ObjectService to be (re-)loaded
[DEBUG] AppModule.IObjectService BEGIN Analyzing com.packtpub.services.ObjectService
[DEBUG] AppModule.IObjectService   END Analyzing com.packtpub.services.ObjectService
[DEBUG] AppModule.IObjectService Invoking constructor public com.packtpub.services.ObjectService() (for service 'IObjectService')
[INFO] support.ClassPathXmlApplicationContext Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b905255: startup date [Tue Jan 08 21:05:19 MSK 2013]; root of context hierarchy
[INFO] xml.XmlBeanDefinitionReader Loading XML bean definitions from class path resource [WEB-INF/applicationContext.xml]
[INFO] support.DefaultListableBeanFactory Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@989472: defining beans [dataSource,namingStrategy,sessionFactory,extendedFinderNamingStrategy,finderIntroductionAdvisor,abstractDaoTarget,abstractDao,objectDao]; root of factory hierarchy
[INFO] datasource.DriverManagerDataSource Loaded JDBC driver: com.mysql.jdbc.Driver
[INFO] support.DefaultListableBeanFactory Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@989472: defining beans [dataSource,namingStrategy,sessionFactory,extendedFinderNamingStrategy,finderIntroductionAdvisor,abstractDaoTarget,abstractDao,objectDao]; root of factory hierarchy
[ERROR] ioc.Registry Error creating bean with name 'sessionFactory' defined in class path resource [WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider
[ERROR] ioc.Registry Operations trace:
[ERROR] ioc.Registry [ 1] Triggering event 'action' on Index:increment
[ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception: Error creating bean with name 'sessionFactory' defined in class path resource [WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider
org.apache.tapestry5.ioc.internal.OperationException: Error creating bean with name 'sessionFactory' defined in class path resource [WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider [at classpath:com/packtpub/pages/Index.tml, line 19]
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.logAndRethrow(OperationTrackerImpl.java:121)
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:88)
    at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:87)
    at org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1124)
    at org.apache.tapestry5.internal.structure.ComponentPageElementResourcesImpl.invoke(ComponentPageElementResourcesImpl.java:146)
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.triggerContextEvent(ComponentPageElementImpl.java:1053)
    at org.apache.tapestry5.internal.services.ComponentEventRequestHandlerImpl.handle(ComponentEventRequestHandlerImpl.java:81)
    at org.apache.tapestry5.internal.services.ImmediateActionRenderResponseFilter.handle(ImmediateActionRenderResponseFilter.java:42)
    at $ComponentEventRequestHandler_11e927111127.handle(Unknown Source)
    at org.apache.tapestry5.internal.services.AjaxFilter.handle(AjaxFilter.java:42)
    at $ComponentEventRequestHandler_11e927111127.handle(Unknown Source)
    at org.apache.tapestry5.services.TapestryModule$41.handle(TapestryModule.java:2476)
    at $ComponentEventRequestHandler_11e927111127.handle(Unknown Source)
    at $ComponentEventRequestHandler_11e9271110ae.handle(Unknown Source)
    at org.apache.tapestry5.internal.services.ComponentRequestHandlerTerminator.handleComponentEvent(ComponentRequestHandlerTerminator.java:43)
    at org.apache.tapestry5.services.InitializeActivePageName.handleComponentEvent(InitializeActivePageName.java:39)
    at $ComponentRequestHandler_11e9271110b0.handleComponentEvent(Unknown Source)
    at $ComponentRequestHandler_11e92711107b.handleComponentEvent(Unknown Source)
    at org.apache.tapestry5.internal.services.ComponentEventDispatcher.dispatch(ComponentEventDispatcher.java:46)
    at $Dispatcher_11e92711107e.dispatch(Unknown Source)
    at $Dispatcher_11e927111078.dispatch(Unknown Source)
    at org.apache.tapestry5.services.TapestryModule$RequestHandlerTerminator.service(TapestryModule.java:302)
    at org.apache.tapestry5.internal.services.RequestErrorFilter.service(RequestErrorFilter.java:26)
    at $RequestHandler_11e927111079.service(Unknown Source)
    at org.apache.tapestry5.services.TapestryModule$3.service(TapestryModule.java:902)
    at $RequestHandler_11e927111079.service(Unknown Source)
    at org.apache.tapestry5.services.TapestryModule$2.service(TapestryModule.java:892)
    at $RequestHandler_11e927111079.service(Unknown Source)
    at org.apache.tapestry5.internal.services.StaticFilesFilter.service(StaticFilesFilter.java:90)
    at $RequestHandler_11e927111079.service(Unknown Source)
    at com.packtpub.services.AppModule$1.service(AppModule.java:98)
    at $RequestFilter_11e927111074.service(Unknown Source)
    at $RequestHandler_11e927111079.service(Unknown Source)
    at $RequestHandler_11e92711106d.service(Unknown Source)
    at org.apache.tapestry5.services.TapestryModule$HttpServletRequestHandlerTerminator.service(TapestryModule.java:253)
    at org.apache.tapestry5.internal.gzip.GZipFilter.service(GZipFilter.java:53)
    at $HttpServletRequestHandler_11e92711106f.service(Unknown Source)
    at org.apache.tapestry5.internal.services.IgnoredPathsFilter.service(IgnoredPathsFilter.java:62)
    at $HttpServletRequestFilter_11e92711106b.service(Unknown Source)
    at $HttpServletRequestHandler_11e92711106f.service(Unknown Source)
    at org.apache.tapestry5.services.TapestryModule$1.service(TapestryModule.java:852)
    at $HttpServletRequestHandler_11e92711106f.service(Unknown Source)
    at $HttpServletRequestHandler_11e92711106a.service(Unknown Source)
    at org.apache.tapestry5.TapestryFilter.doFilter(TapestryFilter.java:171)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
    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: org.apache.tapestry5.runtime.ComponentEventException: Error creating bean with name 'sessionFactory' defined in class path resource [WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider [at classpath:com/packtpub/pages/Index.tml, line 19]
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.processEventTriggering(ComponentPageElementImpl.java:1136)
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.access$3100(ComponentPageElementImpl.java:61)
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl$5.invoke(ComponentPageElementImpl.java:1057)
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl$5.invoke(ComponentPageElementImpl.java:1054)
    at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
    ... 58 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    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.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.packtpub.services.ObjectService.AddObject(ObjectService.java:27)
    at $IObjectService_11e92711112a.AddObject(Unknown Source)
    at $IObjectService_11e9271110ab.AddObject(Unknown Source)
    at com.packtpub.pages.Index.onActionFromIncrement(Index.java:61)
    at com.packtpub.pages.Index.dispatchComponentEvent(Index.java)
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.dispatchEvent(ComponentPageElementImpl.java:927)
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.processEventTriggering(ComponentPageElementImpl.java:1112)
    ... 62 more
Caused by: java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
    at java.lang.Class.privateGetPublicMethods(Class.java:2562)
    at java.lang.Class.getMethods(Class.java:1427)
    at java.beans.Introspector.getPublicDeclaredMethods(Introspector.java:1261)
    at java.beans.Introspector.getTargetMethodInfo(Introspector.java:1122)
    at java.beans.Introspector.getBeanInfo(Introspector.java:414)
    at java.beans.Introspector.getBeanInfo(Introspector.java:161)
    at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:224)
    at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:149)
    at org.springframework.beans.BeanWrapperImpl.getCachedIntrospectionResults(BeanWrapperImpl.java:324)
    at org.springframework.beans.BeanWrapperImpl.getPropertyDescriptorInternal(BeanWrapperImpl.java:354)
    at org.springframework.beans.BeanWrapperImpl.isWritableProperty(BeanWrapperImpl.java:430)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1362)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    ... 79 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    ... 95 more
[INFO] AppModule.TimingFilter Request time: 714 ms

1 个答案:

答案 0 :(得分:1)

根据该异常,您有冲突的罐子。你还没有发布完整的pom,但是我看到你把弹簧2.0(spring-jdbc)与弹簧3.1.1罐混合在一起。您需要查看整个依赖关系树并找出冲突的hibernate jar的来源。使用以下maven命令查看依赖关系树: mvn dependency:tree