大四了,现在在公司实习,乘着空余时间,想把毕设做的好看些,开始搭建框架,但是遇到点问题
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in file [C:\Program Files\Apache Software Foundation\tomcat-7.0.56\wtpwebapps\lfdcwtjxt\WEB-INF\classes\configs\applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.unwrap(DriverManagerConnectionProviderImpl.java:268)
at org.springframework.orm.hibernate4.SessionFactoryUtils.getDataSource(SessionFactoryUtils.java:106)
at org.springframework.orm.hibernate4.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
... 22 more
这个上面显示应该是在spring配置文件中的事务管理器那块出的问题,求解决
Spring的配置文件:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:configs/hibernate.cfg.xml</value>
</property>
</bean>
<!-- 定义事物管理器,并位事物管理器配置上述所定义的session-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 对事物管理器进行设置
表示对save、del、update开头的方法应用事物
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
hibernate配置文件
<session-factory>
<!-- 方言类 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
<!--连接MySQL的IP地址及端口号,注意MySQL的默认远程连接是关闭的-->
jdbc:mysql://localhost:3306/lfdcwtjxt
</property>
<!--连接MySQL的用户名-->
<property name="connection.username">root</property>
<!--连接MySQL的密码-->
<property name="connection.password">root</property>
<!--连接MySQL的驱动类名-->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- hibernate4已经不能用下面的配置了
<property name="current_session_context_class">thread</property>
<property name="org.springframework.orm.hibernate4.SpringSessionContext">thread</property>
-->
<!--以格式良好的方式显示SQL语句-->
<property name="format_sql">true</property>
<!--显示SQL语句-->
<property name="show_sql">true</property>
</session-factory>
求大神解决,小弟着急
------解决思路----------------------
我可不是什么大牛,也就是个干活的小喽喽
跟你问题一样
这个版 spring3.1 + hiberante4
然后你看下面这个
给定义个datasouce
异常来自这段代码:
if ( ConnectionProvider.class.equals( unwrapType )
------解决思路----------------------
DriverManagerConnectionProviderImpl.class.isAssignableFrom( unwrapType ) ) { return (T) this;} else { throw new UnknownUnwrapTypeException( unwrapType );}
这里指明,Connection 需要从 javax.sql.DataSource 中获取。
请确保删除 hibernate.cfg.xml 配置文件中定义的数据源,并将 Spring 定义的 DataSource 注入 LocalSessionFactoryBean。
就像这样:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate/hibernate.cfg.xml</value> </property> <property name="dataSource" ref="dataSource" /></bean>