记一次Spring配置事故

2021-07-15 22:04

阅读:725

标签:eof   nal   加载过程   default   完成   注释   prope   targe   jdbc   

在引入Spring的Validated时,需要声明如下bean:
 
@Bean
public
MethodValidationPostProcessor methodValidationPostProcessor() { return new MethodValidationPostProcessor(); }

 

出于偷懒,放在了如下的一个初始化中:
@Configuration
public class ConfigService implements WebMvcConfigurer {
 
.........
 
    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        return new MethodValidationPostProcessor();
    }
 
.........
 
}

 

配置好后,@Validated生效了,但是aop,事务等出现异常。启动日志如下:
2018-08-25 20:03:09 [INFO] [main] [o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker][328] Bean ‘org.springframework.boot.context.properties.ConversionServiceDeducer$Factory‘ of type [org.springframework.boot.context.properties.ConversionServiceDeducer$Factory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-08-25 20:03:09 [INFO] [main] [o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker][328] Bean ‘mybatis-org.mybatis.spring.boot.autoconfigure.MybatisProperties‘ of type [org.mybatis.spring.boot.autoconfigure.MybatisProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-08-25 20:03:09 [INFO] [main] [o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker][328] Bean ‘org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration‘ of type [org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$f57f05de] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-08-25 20:03:09 [INFO] [main] [o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker][328] Bean ‘org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari‘ of type [org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-08-25 20:03:09 [INFO] [main] [o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker][328] Bean ‘spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties‘ of type [org.springframework.boot.autoconfigure.jdbc.DataSourceProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-08-25 20:03:09 [INFO] [main] [o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker][328] Bean ‘dataSource‘ of type [com.zaxxer.hikari.HikariDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-08-25 20:03:10 [INFO] [main] [o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker][328] Bean ‘sqlSessionFactory‘ of type [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-08-25 20:03:10 [INFO] [main] [o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker][328] Bean ‘sqlSessionTemplate‘ of type [org.mybatis.spring.SqlSessionTemplate] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

 

从日志提示的地方看到如下代码:
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) {
            if (!(bean instanceof BeanPostProcessor) && !isInfrastructureBean(beanName) &&
                    this.beanFactory.getBeanPostProcessorCount() this.beanPostProcessorTargetCount) {
                if (logger.isInfoEnabled()) {
                    logger.info("Bean ‘" + beanName + "‘ of type [" + bean.getClass().getName() +
                            "] is not eligible for getting processed by all BeanPostProcessors " +
                            "(for example: not eligible for auto-proxying)");
                }
            }
            return bean;
        }

 

因为问题的引入是已知的,所以,在这里打了断点测试了下进入的条件。发现在加上MethodValidationPostProcessor的bean时,this.beanFactory.getBeanPostProcessorCount()获取到的值为10,不加这个bean时,获取到的值为21.那有问题,肯定是构造完成后,处理流程差异导致的问题。从构造函数开始寻找BeanPostProcessorChecker的初始化地点。
从这里找到PostProcessorRegistrationDelegate的registerBeanPostProcessors方法,
        int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
        beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
 
        // Separate between BeanPostProcessors that implement PriorityOrdered,
        // Ordered, and the rest.

 

从注释中可以看出,在初始化BeanPostProcessorChecker后,又继续初始化高优先级的,有顺序要求的,然后是剩下的BeanPostProcessor。
往下看:
        // First, register the BeanPostProcessors that implement PriorityOrdered.
        sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
        registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
 
        // Next, register the BeanPostProcessors that implement Ordered.
        List 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.

 

在初始化高优先的BeanPostProcessor后,开始load带有顺序条件的BeanPostProcessor,准备初始化。
回到最开始,我们的MethodValidationPostProcessor的父类实现了Ordered接口。故也在这个for循环的加载过程中load进来,此时,在加载的时候出现问题。
在如下代码块中
@Configuration
public class ConfigService implements WebMvcConfigurer {
    @Resource
    private LogService logService;
 
    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        return new MethodValidationPostProcessor();
    }
}

 

我们注入了业务定义的bean,而这些bean是由最低优先级的BeanPostProcessor来加载并完成初始化的。但此时,为了加载其中的MethodValidationPostProcessor,导致不得不优先装载低优先级bean,此时,aop处理器,数据库处理器等都未完成装载,故由这部分业务bean牵扯到的相关逻辑的aop初始化,注解事务初始化,都事实上失败了。但spring就提示了一个INFO级别的提示,然后剩下的bean由最低优先级的BeanPostProcessor正常处理。
 
问题找到后,解决的方式很简单,由框架层初始化的bean,不要牵扯到业务层。不然即便初始化成功,也会导致一些模块因为顺序的缘故,未完成合适的处理流程,比如aop。
针对我这里的问题,如下解决:
@Configuration
public class MethodValidation {
 
    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        return new MethodValidationPostProcessor();
    }
}

 

重新创建一个类,单独初始化此类即可。 

记一次Spring配置事故

标签:eof   nal   加载过程   default   完成   注释   prope   targe   jdbc   

原文地址:https://www.cnblogs.com/asfeixue/p/9535851.html


评论


亲,登录后才可以留言!