问题描述
applicationContent.xml配置<?xml version="1.0" encoding="UTF-8"?><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:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><context:annotation-config /><context:component-scan base-package="org.tarena"></context:component-scan><!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> <property name="driverClassName"value="com.mysql.jdbc.Driver" /> <property name="url"value="jdbc:mysql://localhost:3306/jd1107db" /> <propertyname="username" value="root" /> <property name="password" value="1234"/> </bean>--><!--占位符配置 --><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><value>classpath:jdbc.properties</value></property></bean><bean id="dataSource" destroy-method="close"class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><bean id="mySessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- <property name="annotatedClasses"><list><value>pojo.User</value></list></property>--><!-- --><property name="packagesToScan"><list><value>org.tarena.pojo</value></list></property> <property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props></property></bean><!-- 事务管理 --><bean id="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="mySessionFactory" /></bean><aop:config><aop:pointcut expression="execution(public * service..*.*(..))" id="serviceOperation"/><aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/></aop:config><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="add"/></tx:attributes></tx:advice><!-- Template --><bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="mySessionFactory"></property></bean></beans>出错提示:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: javax/persistence/Entityat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)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:563)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:377)at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:278)at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3934)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4429)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:526)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:987)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:909)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:495)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1206)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:314)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)at org.apache.catalina.core.StandardService.start(StandardService.java:516)at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)at org.apache.catalina.startup.Catalina.start(Catalina.java:583)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) 问题补充:morgan117 写道
解决方案
那应该就是jar包冲突的问题了。。你去下载最新的hibernante的jar包。把原来的jar删掉。把新的放进去
解决方案二:
<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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <context:annotation-config/> 加上这句然后就妥了你看错误,上面说,不能通过那个name创建bean,加上这句就妥了.是不是很神奇.我也这么觉得.还是配置文件(struts或applycationContext)中class的没有写全路径,default-autowire="byName"的作用就是通过名字获取其bean.想吐血的,求赞.包冲突或者少包的情况下,也可能出现这种问题.具体问题具体分析.此外,如果LocalSessionFactoryBean在Spring中的名称不是"sessionFactory",会抛出找不到你定义的“mySessionFactory”.
解决方案三:
我加了楼上所说的架包,还是出现了错误2014-05-20 20:55:57,084 [main] ERROR [org.springframework.web.context.ContextLoader] - Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/E:/myeclipse_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/spring_struts2_hibernate/WEB-INF/classes/spring/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.ClassFormatError: Unknown constant tag 111 in class file org/apache/commons/collections/map/LRUMapat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)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.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)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:3972)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)at org.apache.catalina.core.StandardService.start(StandardService.java:519)at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)at org.apache.catalina.startup.Catalina.start(Catalina.java:581)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)Caused by: java.lang.ClassFormatError: Unknown constant tag 111 in class file org/apache/commons/collections/map/LRUMapat java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)at java.lang.ClassLoader.defineClass(ClassLoader.java:616)at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2527)at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1010)at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1483)at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)at org.hibernate.util.SimpleMRUCache.init(SimpleMRUCache.java:71)at org.hibernate.util.SimpleMRUCache.<init>(SimpleMRUCache.java:55)at org.hibernate.engine.query.QueryPlanCache.<init>(QueryPlanCache.java:76)at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:239)at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)... 27 more
解决方案四:
那你把mySessionFactory改为默认名称sessionFactory再试试
解决方案五:
成功的话,别忘了给分
解决方案六:
你试一下,把下面的代码复制到web.xml里<filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>entityManagerFactoryBeanName</param-name><param-value>mySessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>找不到mySessionFactory是因为在org.springframework.orm.hibernate3.LocalSessionFactoryBean类在Spring配置文件中的名称有默认值,其默认值为sessionFactory。如果LocalSessionFactoryBean在Spring中的名称不是sessionFactory,就会抛出找不到你定义的“mySessionFactory”的例外
解决方案七:
javax/persistence/Entity这个类找不到。应该是少了ejb3-persistence.jar这个包。加进去就好了吧。