spring 事务传播
2016-11-02 15:07
393 查看
在service中有两个方法 A和 B,
A没有配置事务,B配事务,
若在A中调用B方法时,事务将不起作用,原因是因为spring的事务是基于AOP的方式,是代理类加的增强,Proxy.B,若在内部调用时则仅是直接调用B,事务将失效。
解决方法:1 将B移到另一个类中
2 A调用B时不直接调用B,而是调用AOP代理类 的B方法
可以在A实现类中声明A接口 然后加载时将代理类赋给它。
[java] view
plain copy
@Service
public class InjectBeanSelfProcessor implements BeanPostProcessor , ApplicationContextAware {
private ApplicationContext context;
//① 注入ApplicationContext
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof BeanSelfAware) {
if (AopUtils.isAopProxy(bean)) {
LogConstant.debugLog.info("[postProcessAfterInitialization][isAopProxy][beanName = "+beanName+"][className= "+bean.getClass()+"]" );
((BeanSelfAware) bean).setSelf(bean);
} else {
((BeanSelfAware) bean).setSelf(context.getBean(beanName));
LogConstant.debugLog.info("[postProcessAfterInitialization][isNotAopProxy][beanName = "+beanName+"][className= "+bean.getClass()+"]" );
}
}
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
3. 通过Threadlocal暴漏AOP代理对象,
3.1.配置
<aop:aspectj-autoproxy expose-proxy="true"/><!—注解风格支持-->
3.2在A方法中AopContext.currentProxy()).b();
调用B方法
参考博客:http://jinnianshilongnian.iteye.com/blog/1487235
http://jinnianshilongnian.iteye.com/blog/1492424
A没有配置事务,B配事务,
若在A中调用B方法时,事务将不起作用,原因是因为spring的事务是基于AOP的方式,是代理类加的增强,Proxy.B,若在内部调用时则仅是直接调用B,事务将失效。
解决方法:1 将B移到另一个类中
2 A调用B时不直接调用B,而是调用AOP代理类 的B方法
可以在A实现类中声明A接口 然后加载时将代理类赋给它。
[java] view
plain copy
@Service
public class InjectBeanSelfProcessor implements BeanPostProcessor , ApplicationContextAware {
private ApplicationContext context;
//① 注入ApplicationContext
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof BeanSelfAware) {
if (AopUtils.isAopProxy(bean)) {
LogConstant.debugLog.info("[postProcessAfterInitialization][isAopProxy][beanName = "+beanName+"][className= "+bean.getClass()+"]" );
((BeanSelfAware) bean).setSelf(bean);
} else {
((BeanSelfAware) bean).setSelf(context.getBean(beanName));
LogConstant.debugLog.info("[postProcessAfterInitialization][isNotAopProxy][beanName = "+beanName+"][className= "+bean.getClass()+"]" );
}
}
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
3. 通过Threadlocal暴漏AOP代理对象,
3.1.配置
<aop:aspectj-autoproxy expose-proxy="true"/><!—注解风格支持-->
3.2在A方法中AopContext.currentProxy()).b();
调用B方法
参考博客:http://jinnianshilongnian.iteye.com/blog/1487235
http://jinnianshilongnian.iteye.com/blog/1492424
相关文章推荐
- Spring的七个事务传播行为
- spring 五个事务隔离级别和七个事务传播行为
- spring的AOP简介与事务传播特性总结(转载)
- Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别
- spring事务传播方式
- Spring事务的传播行为
- spring事务传播属性和隔离级别
- Spring中事务传播【1】
- Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别
- Spring事务的传播特性和隔离级别
- Spring事务传播行为种类
- spring 事务的传播特性
- Spring事务配置及事务传播隔离级别
- Spring 事务的传播行为和隔离级别
- SPRING的事务传播特性
- 理解spring事务传播机制
- Spring事务的传播行为
- spring里面事务的传播属性和事务隔离级别
- java三大框架之spring事务传播行为
- spring事务的传播方式