调用本类中标注@Transactional、@Async以及@Valid的方法等注解不生效的解决办法

不生效的原因不做探讨,只讨论解决办法,共三种(第四种AopContext.currentProxy()未成功)

  1. (不推荐)最简单的就是将此方法方法移到其他类中 。这种方法奏效,但是无缘无故需要多建立一个service(接口、实现类)。
  2. 注入ApplicationContext获取bean
/**
 * 技能service实现类
 */
@Service
@Slf4j
public class AbilityServiceImpl implements IAbilityService {

    @Autowired
    private ApplicationContext applicationContext;

	@Transactional
    @Override
    public void saveOne(Ability ability) {
        System.out.println("通过校验");
        // 进行保存操作等...
    }

    /**
     * 批量保存EXCEL中的数据
     */
    @Override
    public void saveOnesFromExcel() {
        List<Ability> data = readFromExcel();
        for (int i = 0, size = data.size(); i < size; i ++) {
            try {
				applicationContext.getBean(IAbilityService.class).saveOne(data.get(i));
                System.out.println("第" + i + "条记录保存成功");
            } catch (ConstraintViolationException e) {
                log.error("第" + i + "条记录违法约束:" + e.getMessage());
            } catch (Exception e) {
                log.error("第" + i + "条记录保存失败");
            }
        }
    }

    /**
     * 从EXCEL中读取
     * @return
     */
    private List<Ability> readFromExcel() {
        return Lists.newArrayList(new Ability(null, null, (byte)1),
                new Ability(null, "测试描述", null),
                new Ability("测试名称", null, null),
                new Ability("约德尔诱捕器", "布置一个陷阱,陷阱可以束缚敌方英雄2秒并将目标暴露在己方视野内3秒。", (byte)1));
    }
}
  1. 通过注入自己来获取当前类的实例,再调用该实例的方法。需要加@Lazy注解防止自我注入时spring抛出的异常org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.env.Environment' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
package com.beemo.demo3.service.impl;

import com.beemo.demo3.entity.Ability;
import com.beemo.demo3.service.IAbilityService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.validation.ConstraintViolationException;
import java.util.List;

/**
 * 技能service实现类
 */
@Service
@Slf4j
public class AbilityServiceImpl implements IAbilityService {

    @Autowired
    @Lazy
    private IAbilityService abilityService;

    @Override
    @Transactional
    public void saveOne(Ability ability) {
        System.out.println("通过校验");
        // 进行保存操作等...
    }

    /**
     * 批量保存EXCEL中的数据
     */
    @Override
    public void saveOnesFromExcel() {
        List<Ability> data = readFromExcel();
        for (int i = 0, size = data.size(); i < size; i ++) {
            try {
                abilityService.saveOne(data.get(i));
                System.out.println("第" + i + "条记录保存成功");
            } catch (ConstraintViolationException e) {
                log.error("第" + i + "条记录违法约束:" + e.getMessage());
            } catch (Exception e) {
                log.error("第" + i + "条记录保存失败");
            }
        }
    }

    /**
     * 从EXCEL中读取
     * @return
     */
    private List<Ability> readFromExcel() {
        return Lists.newArrayList(new Ability(null, null, (byte)1),
                new Ability(null, "测试描述", null),
                new Ability("测试名称", null, null),
                new Ability("约德尔诱捕器", "布置一个陷阱,陷阱可以束缚敌方英雄2秒并将目标暴露在己方视野内3秒。", (byte)1));
    }
}

你可能感兴趣的:(调用本类中标注@Transactional、@Async以及@Valid的方法等注解不生效的解决办法)