spring 事务未生效的解决方案

spring事务未生效有较多的原因造成, 如: 自调用时, 未使用spring代理的对象调用;  方法不是公共、非静态的; 事务的传播机制等。

以下主要讨论未使用spring代理对象调用的情况。

spring的事务是基于AOP的动态代理来完成的, 通过各种增强完成事务的开启,事务的提交,事务的回滚等操作。 开发过程中经常会出现在一个类中调用自身某个方法的情况,这种情况我们称之为“自调用”, 因自调用使用的是当前对象, 而当前的对象并未被spring所管理, 所以AOP对其并没有增强的操作。 该问题的解决之道便是拿到spring容器中管理的bean, 通过该bean调用其他有事务的方法。 

获取spring容器管理的bean有以下几种方式:

1. 获取当前AOP代理的对象

// 在springboot的启动类上添加以下注解,以便从AOP上下文中获取代理对象, springmvc项目则可以通过xml配置的方式,开启相应的功能
@EnableAspectJAutoProxy(exposeProxy = true)

// 在UserService类的方法中, 获取AOP当前的代理对象, 然后再调用有事务的方法
// 获取AOP当前的代理对象
UserService userService = (UserService)AopContext.currentProxy();
// 调用有事务的方法
userService.methodB();

2. 获取应用上下文(ioc容器), 然后从上下文中获取bean

2.1 通过request最终获取到应用上下文

   public void methodD(HttpServletRequest request){
        // 方式一, 通过request获取ServletContext,进而获取ApplicationContext, 最终获取到spring容器中的bean
        ServletContext servletContext = request.getServletContext();
        // 获取应用上下文
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        // 获取容器中的bean
        UserService userService = applicationContext.getBean(UserService.class);
        System.out.println(userService);
        //使用容器中的bean
        userService.methodB();
    }

2.2  通过WebApplicationContext最终获取应用上下文


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;

/**
 * @author changez
 * @desc 获取容器中的bean
 * @datetime 2019-7-23 23:13
 */
// 纳入到spring容器, 否则获取不到
@Component
public class ApplicationContextUtils {

    private static ApplicationContext applicationContext;

    private static WebApplicationContext webApplicationContext;

    // 容器启动时,会注入WebApplicationContext, 作为工具类, 应该将方法,属性的设置为statis便于调用, 
    // 静态属性, spring不能直接注入,所以采用set的方式给静态属性设置值
    @Autowired
    public void setWebApplicationContext(WebApplicationContext webApplicationContext){
        ApplicationContextUtils.webApplicationContext = webApplicationContext;
    }


    public static ApplicationContext getApplicationContext(){

        if (applicationContext == null) {
            // 获取ServletContext
            ServletContext servletContext = webApplicationContext.getServletContext();
            applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        }

        return applicationContext;
    }

    public static Object getBean(String beanName){
        return getApplicationContext().getBean(beanName);
    }

    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

}

使用应用上下文,获取bean

    public void methodE(){
        // 获取容器中的bean
        UserService userService = ApplicationContextUtils.getBean(UserService.class);
        System.out.println(userService);
        // 使用容器中的bean
        userService.methodB();
    }

2.3. 实现ApplicationContextAware 接口

// 纳入到spring容器, 否则获取不到
@Component
public class ApplicationContextUtil2 implements ApplicationContextAware {

    private static ApplicationContext APPLICATION_CONTEXT;

    public static ApplicationContext getApplicationContext() {
        return APPLICATION_CONTEXT;
    }

    /**
     * 容器启动时调用该方法, 将上下文传进来
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        APPLICATION_CONTEXT = applicationContext;
    }
}

使用应用上下文获取bean

   public void methodF(){
        UserService userService = ApplicationContextUtil2.getApplicationContext().getBean(UserService.class);
        System.out.println(userService);
        userService.methodB();
    }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值