论坛首页 Java企业应用论坛

基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。

浏览 25449 次
该帖已经被评为精华帖
作者 正文
   发表时间:2012-10-22  
dafa1892 写道
@Transactiona 加在具体的方法上面不起作用,加在实现类上才起作用,这是怎么回事?郁闷啊。

得看你怎么配置的 方便贴下代码吗
0 请登录后投票
   发表时间:2012-10-23  
我把@Transactiona 加在一个没有实现接口的类上,系统启动的时候报错:
Bean named '***Service' must be of type [com.service.***Service], but was actually of type [$Proxy14]

我的配置文件:
<aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config proxy-target-class="true" />

<tx:annotation-driven  transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
0 请登录后投票
   发表时间:2012-10-23  
dafa1892 写道
我把@Transactiona 加在一个没有实现接口的类上,系统启动的时候报错:
Bean named '***Service' must be of type [com.service.***Service], but was actually of type [$Proxy14]

我的配置文件:
<aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config proxy-target-class="true" />

<tx:annotation-driven  transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

从目前看 <aop:aspectj-autoproxy proxy-target-class="true"/>  代理类 应该是没问题。
你用了springmvc吗? 
能否把那个类定义部分发上来 还有错误信息稍微全点
0 请登录后投票
   发表时间:2012-10-23  
这个是service,没有实现接口。

@Service
public class RoleService extends CommonService<Role> {

   @Transactional
public void save(Role entity) throws Exception {
super.getCommonDao().insert("role.insert",entity);
super.getCommonDao().insert("role.test");
}
}


CommonDao里面是对数据库的操作,继承了SqlSessionDaoSupport。

下面是controllor部分代码
       
         @Resource
        private RoleService roleService;
       
         roleService.save(role);

我是用的spring mvc 持久层用的mybatis,如果我增加一个接口,这个程序是没有问题的。只是我看了你的帖子,想试一下CGLIB动态代理,CGLIB代理不需要实现接口吧。麻烦你了。



0 请登录后投票
   发表时间:2012-10-23  
dafa1892 写道
这个是service,没有实现接口。

@Service
public class RoleService extends CommonService<Role> {

   @Transactional
public void save(Role entity) throws Exception {
super.getCommonDao().insert("role.insert",entity);
super.getCommonDao().insert("role.test");
}
}


CommonDao里面是对数据库的操作,继承了SqlSessionDaoSupport。

下面是controllor部分代码
       
         @Resource
        private RoleService roleService;
       
         roleService.save(role);

我是用的spring mvc 持久层用的mybatis,如果我增加一个接口,这个程序是没有问题的。只是我看了你的帖子,想试一下CGLIB动态代理,CGLIB代理不需要实现接口吧。麻烦你了。




如果用CGLIB是没有问题,代理到类    @Resource  private RoleService 注入是正确的;
目前看说注入失败,原因估计是:
1、<aop:aspectj-autoproxy proxy-target-class="true"/>  修改没同步到服务器
2、spring-mvc配置文件也配置了<aop:aspectj-autoproxy /> 但 proxy-target-class="false"  对于springmvc配置文件应该只加载控制器 而不要加载Service、Dao
请参考http://jinnianshilongnian.iteye.com/blog/1423971
0 请登录后投票
   发表时间:2012-10-24  
谢谢了啊,我查看了一下配置文件,我的spring mvc 的配置文件
<context:component-scan base-package="com.ss">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>


spring的配置文件:
<context:annotation-config/> 
<context:component-scan base-package="com.ss">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan>

如果我把spring mvc 配置文件的中的  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />删除,启动就没有错误了,但是事务不起作用了。这是什么原因啊?
0 请登录后投票
   发表时间:2012-10-24  
dafa1892 写道
谢谢了啊,我查看了一下配置文件,我的spring mvc 的配置文件
<context:component-scan base-package="com.ss">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>


spring的配置文件:
<context:annotation-config/> 
<context:component-scan base-package="com.ss">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan>

如果我把spring mvc 配置文件的中的  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />删除,启动就没有错误了,但是事务不起作用了。这是什么原因啊?

在springmvc配置文件必须排除service dao 只保留controller   此处你没有排除会把之前加载的service覆盖掉  此时就没有事物了


电脑坏了。。。。送修了  好了再给你完整答复   你可以仔细看看那篇文章
0 请登录后投票
   发表时间:2012-10-26  
谢谢了啊,我重新搭了一个环境怎么做都是好用的,在原来那个怎么弄都不好用,是不是jar有问题啊。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics