关于AnnotationSessionschedulerfactorybeann和LocalSessionschedulerfactorybeann的区别

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&大神帮我看看 spring 整合 hibernate4 时sessionFactory 的问题
[问题点数:40分]
大神帮我看看 spring 整合 hibernate4 时sessionFactory 的问题
[问题点数:40分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。spring3.1和hibernate4.0的有关配置及注意事项
这两天使用了一下最新版本的spring3.1和hibernate4 &
发现hibernate4 不是很好用
&所以又返回了使用hibernate3.x
分享一下spring3.1和hibernate4
&的整合经验
Spring3的配置
从官网下载的spring3.1&
在使用的时候&
报缺少jar包的错误
还需要导入另外两个jar包
&aopalliance-alpha1.jar
&&.aopalliance-1.0.0.jar
org/aopalliance/intercept/MethodInterceptor&
Spring3.0 运行问题
报的错误是:java.lang.NoClassDefFoundError:&org/aopalliance/intercept/MethodInterceptor
上网查了下,这可能是spring3.0特有的问题,2.5就没有!出错的原因是少了个包,解决的方案是去
下载aopalliance-alpha1.jar,加到路径里,就行了!
spring3.0比2.5需要好多额外的包,除了上面的aopalliance外,还可能会需要asm和cglib!
下载加入了aopalliance-alpha1.jar,进行测试,再次抛出异常:
Caused by:
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class
[org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]:
Constru nested
exception is java.lang.NoClassDefFoundError:&org/aopalliance/aop/Advice
Caused by: java.lang.NoClassDefFoundError:
org/aopalliance/aop/Advice
上网查看,发现又少了 com.springsource.org.aopalliance-1.0.0.jar
到页面下载
com.springsource.org.aopalliance-1.0.0.jar下载地址:
ce.org.aopalliance-1.0.0.jar
关于hibernate4的配置
先是SessionFactory
的org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
要改成org.springframework.orm.hibernate4.LocalSessionFactoryBean
还以为只要把 hibernate3
换成4就可以了,结果发现spring 3.1里没有这个类了.
(hibernate4的javadoc里写的清清楚楚!!)
annotation也是使用的LocalSessionFactoryBean.这个不改会报
找不到类的错误.
接着把org.springframework.orm.hibernate3.HibernateTransactionManager
org.springframework.orm.hibernate4.HibernateTransactionManager
然后移除hibernateTemplate.这个很重要,spring 3.1不再支持hibernateTemplate了.不晓得什么原因...没深究
然后就是大面积的修改类了...
因为不再使用 hibernateTemplate,都要改成
sessionFactory.getCurrentSession()...
这个转变其实也不算难,唯一有些区别的是对
DetachedCriteria
hibernateTemplate.findByCriteria
(DetachedCriteria,first,max)
换成DetachedCriteria.getExecutableCriteria(sessionFactory.getCurrentSession()).setFirstResult(first).setMaxResults(max).list()
直接使用sessionFactory.getCurrentSession().createCriteria(XXX.class)
代替 DetachedCriteria
是最简单的了.
去看看 hibernate的官方帮助里面有很详细的说明,足以轻松替换掉
hibernateTemplate ,
改完基本也就升级完成了.
配置成功后
在使用session的时候又发现各种各样的错误!最后果断的放弃了hibernate4
即整合spring3与hibernate3
但是同样也发现了几个小错误
出现的问题解决:
问题1:Exception
in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/logging/LogFactory&at
org.springframework.util.ClassUtils.&clinit&()&&
org.springframework.core.io.DefaultResourceLoader.&init&()
org.springframework.context.support.AbstractApplicationContext.&init&()
org.springframework.context.support.AbstractRefreshableApplicationContext.&init&()
org.springframework.context.support.AbstractXmlApplicationContext.&init&()&&
需要加上:commons-logging.jar
log4j-1.2.11.jar
问题2:Exception
in thread "main":
Unexpected exception parsing XML document from class path resource
[text.xml]; nested exception is
java.lang.NoClassDefFoundError:
Caused by:
java.lang.NoClassDefFoundError:
at java.lang.Class.forName0()
需要加上:aspectjweaver.jar
问题3:Exception
in thread "main":
Error creating bean with name 'logBean' defined in class path
resource [text.xml]: Initializ nested
exception is:
Cannot proxy target class because CGLIB2 is not available. Add
CGLIB to the class path or specify proxy
interfaces.
Cannot proxy target class because CGLIB2 is not available. Add
CGLIB to the class path or specify proxy
interfaces.
需要加上:cglib-2.1.3.jar
问题4:xception
in thread "main":
Error creating bean with name 'logBean' defined in class path
resource [text.xml]: Initializ nested
exception is java.lang.NoClassDefFoundError:
org/objectweb/asm/Type
Caused by:
java.lang.NoClassDefFoundError:
org/objectweb/asm/Type
at net.sf.cglib.core.TypeUtils.parseType()
需要加上:asm.jar
一下是我用 spring 3.0
处理事务的时候出现的异常情况,经过一天的查找,发现问题其实很简单:
&&!-- 配置事务管理器
id="transactionManager"
&&class="org.springframework.orm.hibernate3.HibernateTransactionManager"&
&&&&property
name="sessionFactory"&
bean="sessionFactory" /&
&&&&/property&
&&!-- 使Spring关注Annotation
&&context:annotation-config
让Spring通过自动扫描来查询和管理Bean --&
&&context:component-scan
base-package="com.kkks" /&
&&tx:annotation-driven
transaction-manager="transactionManager"
proxy-target-class="true"/&
UserDaoImpl.java
类配置如下
//声明此类为数据持久层的类
@Transactional
@Repository("userDao")
我发现国外的各位牛人真的很厉害啊,我找了半天,人家一下就看出来了
,原文地址在这&
我摘一点关键的:
org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named
'org.springframework.transaction.interceptor.TransactionInterceptor#0'
must be of type [org.aopalliance.aop.Advice], but was actually of
[org.springframework.transaction.interceptor.TransactionInterceptor]
I only get this error
when I've defined&tx:annotation-driven
transaction-manager="transactionManager/&but
I without it, the @Transactional annotation doesn't do anything and
I'm left without a Hibernate session.
Any idea why I get
this error? I'm fairly new to this, but it looks like I'm doing
what the PetClinic example is doing, and I've googled around for
hours and browsed the docs without getting any
Finally, as promised, here
is what I can add of configuration to get the exact same
exception:
&aop:config&
&aop:pointcut id="serviceMethods"
expression="execution(* tld.mydomain.business..*(..))"
&&aop:advisor
advice-ref="txAdvice" pointcut-ref="serviceMethods"
/& &/aop:config&
&tx:advice id="txAdvice"
transactionmanager="transactionManager"
&tx:attributes&
&tx:method name="*" propagation="REQUIRES_NEW"
/& &/tx:attributes&
&/tx:advice&
This again gives me the
following exception:
Caused by:
org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Beannamed 'txAdvice' must be of type [org.aopalliance.aop.Advice],
but was actually of type
[org.springframework.transaction.interceptor.TransactionInterceptor]
I think I see the
problem now:
... must be of type
[org.aopalliance.aop.Advice], but was actually of type
[org.springframework.transaction.interceptor.TransactionInterceptor]
thatTransactionInterceptoris
an implementation oforg.aopalliance.aop.Advice,
this suggests to me that you have a classloading issue.
Specifically, you either have two copies of Spring being loaded by
two different classloaders, or two copies of aop-alliance being
loaded by two different classloaders. In this situation, you can
getinstanceoferrors
orClassCastExceptionsthat
can be a bit hard to diagnose.
I suggests looking
through your manifests and classpaths, including the app-server's
own classpaths, and make sure your application can only find Spring
and aop-alliance in one place. Remember that the Spring JARs
already include a copy of the aop-alliance stuff, so you don't need
another copy.
这就是问题所在了,类加载器加载的时候
发现类型转换错误了,问题就是有两个类加载器 同时加载了,然后就出问题了,我仔细检查了一下classpath 发现
aopalliance-1.0.jar 和 aopalliance-alpha1.jar
两个包冲突了,去掉后者就行了。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。相关热词搜索:
上一篇:下一篇:
延伸阅读:
频道总排行
最新IT新闻
频道本月排行

我要回帖

更多关于 beanfactory 的文章

 

随机推荐