一、问题
今天发现用户注册的Service的事务并没有起到作用,再抛出一个RuntimeException后,并没有发生回滚,下面是调试步骤:
1、检查数据库的引擎是否是innoDB
2、启动类上是否加入@EnableTransactionManagement注解
3、是否在方法上加入@Transactional注解或Service的类上是否有@Transactional注解
4、方法是否为public
5、是否是因为抛出了Exception等checked异常
然而事务失效都不是这些原因引起的,并且发现其他Service的事务都可以正常使用。在查看打印的异常调用链的时候,发现这个Service是没有被AOP代理过的,所以推测可能是因为其他整合Spring的框架提前引用了这个Service。
为了验证,新建了一个Service,并且把代码copy到新建的类中,测试其事务,发现事务可以正常使用,下面是打印的异常信息:
![](https://img-blog.csdn.net/20180725103527209?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZpbmFsY29sYQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
从上面可以明显看到,Spring为这个service生成了代理类,证明事务是可以正常使用的,并且原service的失效应该是其他的整合Spring的框架提前引用造成的。
因为项目还使用了Shiro作为权限管理,并且在编写Shiro的自定义验证器Realm中引用了该UserService,后来把Realm中的Service换成了Mapper后,该Service的事务可以正常使用了。
错误原因:
Spring中事务是通过AOP创建代理对象来完成的,有BeanFactoryTransactionAttributeSourceAdvisor完成对需要事务的方法织入对事务的处理。完成创建AOP代理对象的功能由一个特殊的BeanPostProcessor完成--AnnotationAwareAspectJAutoProxyCreator。该类实现了BeanPostProcessor接口,在bean创建完成并将属性设置好之后,拦截bean,并创建代理对象,在原对象的方法功能上添加增强器中增强方法的处理。对于事务增强器BeanFactoryTransactionAttributeSourceAdvisor而言,也就是在原有方法上加入事务的功能。
但是,在ApplicationContext刷新上下文过程(refresh)中,上下文会调用registerBeanPostProcessors方法将BeanFactory中的所有BeanPostProcessor后处理器注册到BeanFactory中,使其后面流程中创建bean的时候生效。下面是其实现源码:
-
public static void registerBeanPostProcessors(
-
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
-
-
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
-
-
// Register BeanPostProcessorChecker that logs an info message when
-
// a bean is created during BeanPostProcessor instantiation, i.e. when
-
// a bean is not eligible for getting processed by all BeanPostProcessors.
-
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
-
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
-
-
-
-
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
-
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
-
List<String> orderedPostProcessorNames = new ArrayList<>();
-
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
-
for (String ppName : postProcessorNames) {
-
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
-
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
-
priorityOrderedPostProcessors.add(pp);
-
if (pp instanceof MergedBeanDefinitionPostProcessor) {
-
internalPostProcessors.add(pp);
-
-
-
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
-
orderedPostProcessorNames.add(ppName);
-
-
-
nonOrderedPostProcessorNames.add(ppName);
-
-
-
-
-
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
-
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
-
-
-
-
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
-
for (String ppName : orderedPostProcessorNames) {
-
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
-
orderedPostProcessors.add(pp);
-
if (pp instanceof MergedBeanDefinitionPostProcessor) {
-
internalPostProcessors.add(pp);
-
-
-
sortPostProcessors(orderedPostProcessors, beanFactory);
-
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
-
-
// Now, register all regular BeanPostProcessors.
-
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
-
for (String ppName : nonOrderedPostProcessorNames) {
-
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
-
nonOrderedPostProcessors.add(pp);
-
if (pp instanceof MergedBeanDefinitionPostProcessor) {
-
internalPostProcessors.add(pp);
-
-
-
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
-
-
// Finally, re-register all internal BeanPostProcessors.
-
sortPostProcessors(internalPostProcessors, beanFactory);
-
registerBeanPostProcessors(beanFactory, internalPostProcessors);
-
-
// Re-register post-processor for detecting inner beans as ApplicationListeners,
-
// moving it to the end of the processor chain (for picking up proxies etc).
-
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
-
上面流程为:
- 实例化所有实现了PriorityOrdered接口的BeanPostProcessor,排序后注册到BeanFactory
- 实例化所有实现了Ordered接口的BeanPostProcessor,排序后注册到BeanFactory
- 实例化剩余的BeanPostProcessor,注册到BeanFactory
- 注册内部使用的BeanPostProcessor
- 添加一个特殊的后处理器--ApplicationListenerDetector
由于AnnotationAwareAspectJAutoProxyCreator实现了Ordered接口,所以会在第2步中注册到BeanFactory,然后生效,可以拦截bean创建并生成代理对象。但是,在其注册前,有一个同样实现了Ordered接口的MethodValidationPostProcessor。在该类的实例化过程中,会由ValidationAutoConfiguration通过工厂方法来创建,创建过程中,需要传入Environment对象作为参数,然后Spring会从BeanFactory中查找所有符合Environment类型的bean,下面是查询过程:
-
-
private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
-
List<String> result = new ArrayList<>();
-
-
-
for (String beanName : this.beanDefinitionNames) {
-
if (!isAlias(beanName)) {
-
-
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
-
-
if (!mbd.isAbstract() && (allowEagerInit ||
-
(mbd.hasBeanClass() || !mbd.isLazyInit() || isAllowEagerClassLoading()) &&
-
!requiresEagerInitForType(mbd.getFactoryBeanName()))) {
-
-
boolean isFactoryBean = isFactoryBean(beanName, mbd);
-
BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
-
-
(allowEagerInit || !isFactoryBean ||
-
(dbd != null && !mbd.isLazyInit()) || containsSingleton(beanName)) &&
-
-
(dbd != null ? mbd.isSingleton() : isSingleton(beanName))) &&
-
isTypeMatch(beanName, type);
-
if (!matchFound && isFactoryBean) {
-
beanName = FACTORY_BEAN_PREFIX + beanName;
-
-
-
matchFound = (includeNonSingletons || mbd.isSingleton()) && isTypeMatch(beanName, type);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
return StringUtils.toStringArray(result);
-
如果存在FactoryBean的话,在比对过程中会实例化FactoryBean(isTypeMatch中实现)
在isTypeMatch中有这么一段代码:
-
if (FactoryBean.class.isAssignableFrom(beanType)) {
-
if (!BeanFactoryUtils.isFactoryDereference(name) && beanInstance == null) {
-
// 如果是FactoryBean类型,需要对其实例化后才能知道到底它创建的bean是什么类型
-
beanType = getTypeForFactoryBean(beanName, mbd);
-
-
-
-
-
下面是getTypeForFactoryBean中的创建FactoryBean实例部分的代码:
-
FactoryBean<?> fb = (mbd.isSingleton() ?
-
getSingletonFactoryBeanForTypeCheck(beanName, mbd) :
-
getNonSingletonFactoryBeanForTypeCheck(beanName, mbd));
下面是getSingletonFactoryBeanForTypeCheck实现:
-
private FactoryBean<?> getSingletonFactoryBeanForTypeCheck(String beanName, RootBeanDefinition mbd) {
-
synchronized (getSingletonMutex()) {
-
-
BeanWrapper bw = this.factoryBeanInstanceCache.get(beanName);
-
-
return (FactoryBean<?>) bw.getWrappedInstance();
-
-
-
Object beanInstance = getSingleton(beanName, false);
-
if (beanInstance instanceof FactoryBean) {
-
return (FactoryBean<?>) beanInstance;
-
-
if (isSingletonCurrentlyInCreation(beanName) ||
-
(mbd.getFactoryBeanName() != null && isSingletonCurrentlyInCreation(mbd.getFactoryBeanName()))) {
-
-
-
-
-
-
-
beforeSingletonCreation(beanName);
-
instance = resolveBeforeInstantiation(beanName, mbd);
-
-
-
bw = createBeanInstance(beanName, mbd, null);
-
instance = bw.getWrappedInstance();
-
-
-
-
afterSingletonCreation(beanName);
-
-
-
FactoryBean<?> fb = getFactoryBean(beanName, instance);
-
-
this.factoryBeanInstanceCache.put(beanName, bw);
-
-
-
-
从上面代码看以看到,Spring会实例化FactoryBean,以确定其创建的bean的类型
由于ShiroFilterFactoryBean实现了FactoryBean接口,所以它会提前被初始化。又因为SecurityManager,SecurityManager依赖于Realm实现类、Realm实现类又依赖于UserService,所以引发所有相关的bean提前初始化。
ShiroFilterFactoryBean -> SecurityManager
-> Realm实现类 -> UserService
但是此时还只是ApplicationContext中registerBeanPostProcessors注册BeanPostProcessor处理器的阶段,此时AnnotationAwareAspectJAutoProxyCreator还没有注册到BeanFactory中,UserService无法享受到事务处理!
三、解决办法
- 在Realm实现中使用Mapper,而不是直接使用Service对象。缺点:直接和数据库交互,并且也没有Service中的逻辑交互以及缓存
- 在Realm中Service声明上加入@Lazy注解,延迟Realm实现中Service对象的初始化时间,这样就可以保证Service实际初始化的时候会被BeanPostProcessor拦截,创建具有事务功能的代理对象
![](https://img-blog.csdn.net/20180905162515843?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZpbmFsY29sYQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)