springboot中BeanPostProcessor初始化相关问题

PS:禁止拷贝形式转载,转载请以URL形式

1.简介

处理BeanPostProcessor依赖于普通bean@Component、@Bean....,导致普通bean没有被所有BeanPostProcessor进行处理,出现的可能会导致程序错误的提示

is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 

2.参考

https://blog.csdn.net/f641385712/article/details/89737791?utm_medium=distribute.pc_relevant.none-task-blog-title-1&spm=1001.2101.3001.4242

http://www.itcdns.cn/problem/detail/1585891950405

3.总结

  1. BeanPostProcessor接口是Spring对Bean 初始化定义的扩展接口,postProcessBeforeInitialization()bean 初始化前操作方法和postProcessAfterInitialization()bean 初始化后操作方法

  2. bean(BeanPostProcessor) 申明后创建优先于普通bean(@Component、@Bean....

  3. bean(BeanPostProcessor) 如果依赖其他bean ,则加载其他bean后在初始化自身

  4. bean(BeanPostProcessor) 在spring中会被排序进行初始化

    • PriorityOrdered+BeanPostProcessor >
    • Ordered+BeanPostProcessor >
    • BeanPostProcessor >
    • spring 内部BeanPostProcessor >
      image.png
  5. 参考第3、4点,低优先级BeanPostProcessor如果依赖普通bean@Component、@Bean....,则普通bean会被高优先级BeanPostProcessor进行拦截处理,但是同级别或低级别的BeanPostProcessor不会处理该普通bean,这时候控制台就会显示

is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

4. 实例(springboot+shiro)

  1. 环境
    springboot:2.2.8RELEASE
    shiro:1.5.3
  2. 源码
    普通的springboot + shiro


    image.png
  3. 输出
[09:20:40:092] [INFO] - org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker.postProcessAfterInitialization(PostProcessorRegistrationDelegate.java:335) - Bean 'shiroConfig' of type [com.visec.platform.webstarter.security.core.shiro.ShiroConfig$$EnhancerBySpringCGLIB$$e32c1278] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[09:20:40:112] [INFO] - org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker.postProcessAfterInitialization(PostProcessorRegistrationDelegate.java:335) - Bean 'shiroFilterChain' of type [com.visec.platform.webstarter.security.core.shiro.ShiroFilterChain] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[09:20:42:117] [INFO] - org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker.postProcessAfterInitialization(PostProcessorRegistrationDelegate.java:335) - Bean 'customSessionDao' of type [org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[09:20:42:120] [INFO] - org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker.postProcessAfterInitialization(PostProcessorRegistrationDelegate.java:335) - Bean 'customRealm' of type [com.visec.platform.webstarter.security.core.shiro.ShiroRealm] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[09:20:42:125] [INFO] - org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker.postProcessAfterInitialization(PostProcessorRegistrationDelegate.java:335) - Bean 'sessionIdCookie' of type [org.apache.shiro.web.servlet.SimpleCookie] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[09:20:42:132] [INFO] - org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker.postProcessAfterInitialization(PostProcessorRegistrationDelegate.java:335) - Bean 'customSessionManager' of type [org.apache.shiro.web.session.mgt.DefaultWebSessionManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
  1. 原因
    • ShiroFilterFactoryBean实现了FactoryBeanBeanPostProcessor

      image.png

    • ShiroFilterFactoryBean定义在ShiroConfig我们定义的配置文件中且ShiroFilterFactoryBean的创建依赖于SessionDao、sessionIdCookie等,所以控制台输出了这些被ShiroFilterFactoryBean依赖的BEANis not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

    • ShiroFilterFactoryBean实现BeanPostProcessor是为了给所有shiro 相关的filter bean 设置统一的跳转路径(登录成功、登录URL、未授权URL)

      image.png

    • ShiroFilterFactoryBean实现FactoryBean 为了返回shiro 的filter 去统一拦截请求,然后在将请求分发到我们添加到shiro 中的filter 链中,其中返回的filter 也会为我们定义在shiro 中的filter 链根据配置统一设置相关跳转路径

      image.png

  1. 解决
    PS:总结 ShiroFilterFactoryBean 实现FactoryBean为了返回shiro管理filter,同时也为基于shiro的注册的filtershiroFilterFactoryBean.setFilterChainDefinitionMap 进行统一设置公共URL,实现BeanPostProcessor就是为了设置不在ShiroFilterFactoryBean中注册的shiro filter.

5.1 方案A

  1. 所有shiro 相关的filter 都在ShiroFilterFactoryBean.setFilterChainDefinitionMap 中new 出来,
  2. ShiroFilterFactoryBean 进行封装,舍弃掉BeanPostProcessor相关方法,封装bean 为普通Spring bean 对ShiroFilterFactoryBean 进行降级
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.FactoryBean;

public class MyShiroFilterFactoryBean implements FactoryBean<AbstractShiroFilter> {
    private final ShiroFilterFactoryBean shiroFilterFactoryBean;

    public MyShiroFilterFactoryBean(ShiroFilterFactoryBean shiroFilterFactoryBean) {
        this.shiroFilterFactoryBean = shiroFilterFactoryBean;
    }

    public ShiroFilterFactoryBean getShiroFilterFactoryBean() {
        return shiroFilterFactoryBean;
    }

    @Override
    public AbstractShiroFilter getObject() throws Exception {
        return (AbstractShiroFilter) shiroFilterFactoryBean.getObject();
    }

    @Override
    public Class<?> getObjectType() {
        return shiroFilterFactoryBean.getObjectType();
    }

    @Override
    public boolean isSingleton() {
        return shiroFilterFactoryBean.isSingleton();
    }
}
  1. 使用包装bean 进行返回注册Spring 中


    image.png

5.1 方案B

该方案无代码,纯描述

  1. BeanPostProcessorFactoryBean职责分离,可以分为两个类
  2. 分离后BeanPostProcessor 根据配置yml 定义的配置进行赋值 filter,避免其去加载其他bean 导致输出
  3. 分离后FactoryBean 根据配置yml 定义的配置进行赋值filter
    PS总结:通过将依赖的条件分离,且基于配置文件使用@Value方式去读取,避免牵连加载其他bean 导致大量日志输出,该方案较于A,能完全保留shiro 功能(有点鸡肋,只需要规范好shiro filter 都基于FactoryBean管理),但是相对于官方API实现会有较大变更
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容