Spring 启动 @EnableTransactionManagement(mode = AdviceMode.ASPECTJ) 不工作

Spring Boot @EnableTransactionManagement(mode = AdviceMode.ASPECTJ) dont work

当我尝试使用时:

@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)

交易无效,但如果我使用

@EnableTransactionManagement(mode = AdviceMode.PROXY)

一切正常。

我的代码:

SpringBootAplication

@SpringBootApplication
@EnableScheduling
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class SpringBootAplication
{
    private static ConfigurableApplicationContext applicationContext;


    public static void main(String[] args)
    {              System.out.println(InstrumentationLoadTimeWeaver.isInstrumentationAvailable());
        applicationContext = SpringApplication.run(SpringBootAplication.class, args);
    }

控制器

@RestController
public class MyController
{
    @Autowired
    MyRepository repository;

    @Transactional
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public void test()
    {
        repository.findById("name");
        System.out.println(TransactionAspectSupport.currentTransactionStatus());
    }
}

我还有一个 VM 选项:-javaagent:"spring-instrument-5.1.2.RELEASE.jar" for ASPECTJ。

这是我在尝试获取当前交易时接受的异常。

org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope

也是我的依靠

implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-thymeleaf')
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation("org.springframework.boot:spring-boot-starter-websocket")
implementation('org.springframework.boot:spring-boot-starter-security')
implementation("org.springframework.boot:spring-boot-devtools")

像这样添加 aspectjweaver.jar 代理:-javaagent:/path/to/aspectjweaver.jar 应该可以解决您的问题。