spring aop类内部调用不拦截原因及解决方案

spring对应java web开发的同学来说,都不陌生,其中事务@Transactional在service层更是常常使用。

1.aop类内部调用不拦截原因

细心的同学也许早就发现当service中的某个没标注@Transactional的方法调用另一个标注了@Transactional的方法时,居然没开启事务。例如

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,readOnly=false)
	public void insert01(User u){
		this.userMapper.insert(u);
		throw new RuntimeException("测试插入事务");
	}
	public void insert02(User u){
		this.insert01(u);
		throw new RuntimeException("测试插入事务");
	}
当controller或其他service直接调用insert01(User u)时,事务是正常的,数据库里面的确没数据;但是如果调用的是insert02(User u)方法,异常抛出了,但是数据库里面居然有数据,说明事务不正常了。


我们知道@Transactional其实就是一个aop代理,是一个cglib动态代理(常用的动态代理有cglib动态代理,有jdk动态代理)。

直接调用insert01(User u)时,其实流程是这样的:


也就是说controller或其他service首先调用的是AOP代理对象而不是目标对象,首先执行事务切面,事务切面内部通过TransactionInterceptor环绕增强进行事务的增强,即进入目标方法之前开启事务,退出目标方法时提交/回滚事务。

controller或其他service调用insert02(User u)时,流程是这样的:

此处的this指向目标对象,因此调用this.b()将不会执行b事务切面,即不会执行事务增强。

我们通过debugger方式看看controller中的userService和insert02(xxx)中的this,会发现controller中的userService是一个代理对象

看到userService=UserService$$EnhanceBySpringCGLIB$$c2174c0b

这this明显和controller中的userSerivce不是同一个了。

如果同学了解cglib(执行期进行代码植入)或aspectJ(编译期就进行代码植入,反编译后看更清晰了解,看博文)。为了让同学对上面说的代理对象有个更直观的印象,我参考aspectJ那样编译后的代码植入方式,写个UserServiceProxy.java.(spring 对aop的真实处理不是这样的,我这只是举个例子直观说明,有些同学对invoke()方法不直观)
public class UserServiceProxy extends UserService{
	//模拟目标对象实例化
	private UserService targetService = new UserService();
	@Override
	public void insert01(User u) {
		System.out.println("开启事务.....transational starting....");
		targetService.insert01(u);
		System.out.println("结束事务.....transational end......");
	}
	@Override
	public void insert02(User u) {
		targetService.insert01(u);
	}
}

也就是说controller中的userSerivce实际是UserServiceProxy,那么UserServiceProxy.insert02()的时候里面调用的是目标对象UserService.insert01()了,并不是代理对象UserServiceProxy.insert01(),所以事务没有开启。


2.aop类内部调用拦截生效的解决方案

2.1 方案一--从beanFactory中获取对象

刚刚上面说到controller中的UserService是代理对象,它是从beanFactory中得来的,那么service类内调用其他方法时,也先从beanFacotry中拿出来就OK了。

public void insert02(User u){
		getService().insert01(u);
	}
	private UserService getService(){
		return SpringContextUtil.getBean(this.getClass());
	}

2.2 方案二--获取代理对象

private UserService getService(){
		// 采取这种方式的话,
		//@EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true)
		//必须设置为true
		return AopContext.currentProxy() != null ? (UserService)AopContext.currentProxy() : this;
	}
如果aop是使用注解的话,那需要@EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true),如果是xml配置的,把expose-proxy设置为true,如

<aop:config expose-proxy="true">
       <aop:aspect ref="XXX">
          <!-- 省略--->
       </aop:aspect>
</aop:config>


2.3方案三--将项目转为aspectJ项目

将项目转为aspectJ项目,aop转为aspect 类。具体就不写了

2.4 方案四--BeanPostProcessor

通过BeanPostProcessor 在目标对象中注入代理对象,定义InjectBeanSelfProcessor类,实现BeanPostProcessor。也不具体写了

也许还有其他方案。








  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.spring.dao; import org.springframework.stereotype.Component; @Component("userDAO") public class UserDao { public void say() { System.out.println("say method is called"); } public void smile() { System.out.println("smile method is called"); } public void cry() { System.out.println("cry method is called"); } public void jump() { System.out.println("jump method is called"); } } 注意观察包名。@Component("userDAO")等价于在spring配置文件中定义一个<bean id="userDAO"/> 编写Service package com.spring.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.spring.dao.UserDao; @Component("userService") public class UserService { @Resource(name="userDAO") private UserDao dao; public UserDao getDao() { return dao; } public void setDao(UserDao dao) { this.dao = dao; } public void say() { dao.say(); } public void smile() { dao.smile(); } public void cry() { dao.cry(); } public void jump() { dao.jump(); } } 注意观察包名。@Component("userService")等价于在spring配置文件中定义一个<bean id="userService"/> @Resource(name="userDAO")将userDA注入进来 写一个拦截器的 package com.spring.aop; import org.springframework.stereotype.Component; @Component("logIntercepter") public class LogIntercepter { public void before(){ System.out.println("----------before-------------"); } public void after(){ System.out.println("----------after-------------"); } public void exception(){ System.out.println("----------exception-------------"); } public void around(){ System.out.println("----------exception-------------"); } } 注意观察包名。@Component("logIntercepter")等价于在spring配置文件中定义一个<bean id="logIntercepter"/> applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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/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"> <context:annotation-config/> <context:component-scan base-package="com.spring.*"/> <aop:config> <aop:aspect id="aspect" ref="logIntercepter"> <aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/> <aop:before method="before" pointcut-ref="pointCut"/> <aop:after method="after" pointcut-ref="pointCut"/> <aop:after-throwing method="exception" pointcut-ref="pointCut"/> <!-- <aop:around method="around" pointcut-ref="pointCut"/> --> </aop:aspect> </aop:config> </beans><context:annotation-config/> <context:component-scan base-package="com.spring.*"/> 两行为开启spring的注解配置 <aop:aspect id="aspect" ref="logIntercepter"> 引入具体的AOP操作 <aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/>声明一个切入点,注意execution表达式的写法 <aop:before method="before" pointcut-ref="pointCut"/> aop前置通知 <aop:after method="after" pointcut-ref="pointCut"/> aop后置通知, <aop:after-throwing method="exception" pointcut-ref="pointCut"/> aop异常通知 以上结合起来意思就是在调用com.spring.service包或子包下的所有方法之前或之后或抛出异常时依次调用id为logIntercepter的中的before after exception方法 测试用例 package com.spring.test; import javax.annotation.Resource; import org.junit.Test; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import com.spring.service.UserService; @ContextConfiguration(locations="classpath:applicationContext.xml") public class SpringTest extends AbstractJUnit4SpringContextTests { @Resource(name="userService") private UserService userService; @Test public void test1(){ userService.say(); System.out.println(); userService.smile(); System.out.println(); userService.cry(); } } 此单元测试基于spring的AbstractJUnit4SpringContextTests,你需要添加spring的关于单元测试的支持 在上标注@ContextConfiguration(locations="classpath:applicationContext.xml")意思是去classpath路径下加载applicationContext.xml @Resource(name="userService")意思是把userService注入进来 最终输出结果为: ----------before------------- say method is called ----------after------------- ----------before------------- smile method is called ----------after------------- ----------before------------- cry method is called ----------after-------------

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值