Mar*_*ski 8 validation aop spring transactions spring-aspects
如果我@Validated向我的服务的接口/实现添加注释,那么该服务不再是事务性的.Stacktrace显示没有,TransactionInterceptor但我只看到MethodValidationInterceptor.如果我删除@Validated然后我看到TransactionInterceptor并MethodValidationInterceptor消失当然.这些方面是否相互排斥?
@Service
//@Validated <- here is my problem :)
public interface AdminService {
String test(String key, String result);
}
public class AdminServiceImpl implements AdminService, BeanNameAware, ApplicationContextAware {
@Override
@Transactional(transactionManager = "transactionManager")
public String test(String key, String result) {
return "hehe";
}
}
@Configuration
@EnableAspectJAutoProxy(exposeProxy = true)
@EnableTransactionManagement(order = AspectPrecedence.TRANSACTION_MANAGEMENT_ORDER)
public class AppConfiguration {..}
Run Code Online (Sandbox Code Playgroud)
解决了
@Transactional@Validated当服务使用ApplicationContextAware如下界面注入自己的代理时,不要一起工作:
private String beanName;
private AdminService self;
@Override
public void setBeanName(String beanName) {
this.beanName = beanName;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
self = applicationContext.getBean(beanName, AdminService.class);
}
Run Code Online (Sandbox Code Playgroud)
在调试过程中,我注意到在后处理阶段setApplicationContext()调用该方法ApplicationContextAwareProcessor,其中MethodValidationPostProcessor(@Validated)和AnnotationAwareAspectJAutoProxyCreator(@Transactional和@Aspects)将原始bean包装到代理实例中.
getBean()在此过程中间调用方法会导致bean无法完全初始化,因为某些后处理操作未应用.在我的情况下TransactionInterceptor没有添加.
为了将自己的代理注入到服务中,我最终创建了专门的Ordered BeaNPostProcessor,LOWEST_PRECEDENCE以确保我在完全初始化的bean上运行.
| 归档时间: |
|
| 查看次数: |
212 次 |
| 最近记录: |