- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
给定以下服务:
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();
}
...
}
我的问题是:有没有办法将 @EnableTransactionManagement
与 StaticMethodMatcherPointcutAdvisor
结合使用?
解决方法:
@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/
我是一名优秀的程序员,十分优秀!