gpt4 book ai didi

java - 如何将 @EnableTransactionManagement 与 StaticMethodMatcherPointcutAdvisor 结合使用

转载 作者:太空宇宙 更新时间:2023-11-04 07:13:00 25 4
gpt4 key购买 nike

给定以下服务:

public interface MyService {
void method();
}

它的实现:

@Service
public class MyServiceImpl implements MyService {

@Transactional
@CustomAnnotation
@Override
public void method() {
...
}
}

我想按以下方式使用 StaticMethodMatcherPointcutAdvisor:

public class MyPointcutAdvisor extends StaticMethodMatcherPointcutAdvisor {
...

@Override
public boolean matches(Method method, Class targetClass) {
Method m = method;
if(annotationPresent(method)) {
return true;
}
Class<?> userClass = ClassUtils.getUserClass(targetClass);
Method specificMethod = ClassUtils.getMostSpecificMethod(method, userClass);
specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
if(annotationPresent(specificMethod )) {
return true;
}
return false;
}
...
}

问题在于 Spring 使用 InfrastructuralAdvisorAutoProxyCreator 创建该类的代理,而 DefaultAdvisorAutoProxyCreator 将为 MyPointcutAdvisor 创建代理,但 MyPointcutAdvisor 仅将代理作为 targetClass 参数提供。因此,PointcutAdvisor 无法找到注释,因此不匹配。

为了完成,这是我的配置类:

@Configuration
@EnableTransactionManagement
public class MyConfiguration {

@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
return new DefaultAdvisorAutoProxyCreator();
}

@Bean
public MyPointcutAdvisor myPointcutAdvisor() {
return new MyPointcutAdvisor();
}
...
}

我的问题是:有没有办法将 @EnableTransactionManagementStaticMethodMatcherPointcutAdvisor 结合使用?

解决方法:

  • @CustomAnnotation放入服务接口(interface)中:我想要干净的接口(interface)。
  • @Role(BeanDefinition.ROLE_INFRASTRUCTURE)添加到MyPointCutAdvisor bean配置中,这样,InfrastructureAdvisorAutoProxyCreator将创建代理。这似乎是错误的方式,因为这个 bean 不是基础设施
  • ProxyTransactionManagementConfiguration复制bean,删除@EnableTransactionManagement并删除@Role(BeanDefinition.ROLE_INFRASTRUCTURE),因此DefaultAdvisorAutoProxyCreator将创建代理,这是我当前的解决方法,并导致以下配置:

    @Configuration
    public class MyWorkaroundConfiguration {
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
    return new DefaultAdvisorAutoProxyCreator();
    }

    @Bean
    public MyPointcutAdvisor myPointcutAdvisor() {
    return new MyPointcutAdvisor();
    }

    @Bean
    public TransactionAttributeSource transactionAttributeSource() {
    return new AnnotationTransactionAttributeSource();
    }

    @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
    public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(
    TransactionInterceptor transactionInterceptor) {
    BeanFactoryTransactionAttributeSourceAdvisor advisor =
    new BeanFactoryTransactionAttributeSourceAdvisor();
    advisor.setTransactionAttributeSource(transactionAttributeSource());
    advisor.setAdvice(transactionInterceptor);
    return advisor;
    }

    @Bean
    public TransactionInterceptor transactionInterceptor(
    PlatformTransactionManager transactionManager) {
    TransactionInterceptor interceptor = new TransactionInterceptor();
    interceptor.setTransactionAttributeSource(transactionAttributeSource());
    interceptor.setTransactionManager(transactionManager);
    return interceptor;
    }
    ...
    }

最佳答案

使用 @EnableAspectJAutoProxy 而不是 DefaultAutoProxyCreator 对我有用。

@Configuration
@EnableAspectJAutoProxy
@EnableTransactionManagement
public class MyConfiguration {

}

这也允许像 M. Deinum 建议的那样使用 @Aspect。

关于java - 如何将 @EnableTransactionManagement 与 StaticMethodMatcherPointcutAdvisor 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20369938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com