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.参考
http://www.itcdns.cn/problem/detail/1585891950405
3.总结
-
BeanPostProcessor
接口是Spring对Bean 初始化定义的扩展接口,postProcessBeforeInitialization()
bean 初始化前操作方法和postProcessAfterInitialization()
bean 初始化后操作方法 -
bean(
BeanPostProcessor
) 申明后创建优先于普通bean(@Component、@Bean....
) -
bean(
BeanPostProcessor
) 如果依赖其他bean ,则加载其他bean后在初始化自身 -
bean(
BeanPostProcessor
) 在spring中会被排序进行初始化-
PriorityOrdered
+BeanPostProcessor
> -
Ordered
+BeanPostProcessor
> -
BeanPostProcessor
> - spring 内部
BeanPostProcessor
>
image.png
-
-
参考第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)
- 环境
springboot:2.2.8RELEASE
shiro:1.5.3
-
源码
普通的springboot + shiro
image.png - 输出
[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)
- 原因
-
image.pngShiroFilterFactoryBean
实现了FactoryBean
和BeanPostProcessor
-
而
ShiroFilterFactoryBean
定义在ShiroConfig
我们定义的配置文件中且ShiroFilterFactoryBean
的创建依赖于SessionDao、sessionIdCookie
等,所以控制台输出了这些被ShiroFilterFactoryBean
依赖的BEANis not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
-
image.pngShiroFilterFactoryBean
实现BeanPostProcessor
是为了给所有shiro 相关的filter bean 设置统一的跳转路径(登录成功、登录URL、未授权URL)
-
image.pngShiroFilterFactoryBean
实现FactoryBean
为了返回shiro 的filter 去统一拦截请求,然后在将请求分发到我们添加到shiro 中的filter 链中,其中返回的filter 也会为我们定义在shiro 中的filter 链根据配置统一设置相关跳转路径
-
- 解决
PS:总结ShiroFilterFactoryBean
实现FactoryBean
为了返回shiro管理filter,同时也为基于shiro的注册的filtershiroFilterFactoryBean.setFilterChainDefinitionMap
进行统一设置公共URL,实现BeanPostProcessor
就是为了设置不在ShiroFilterFactoryBean
中注册的shiro filter.
5.1 方案A
- 所有shiro 相关的filter 都在
ShiroFilterFactoryBean.setFilterChainDefinitionMap
中new 出来, - 对
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();
}
}
-
使用包装bean 进行返回注册Spring 中
image.png
5.1 方案B
该方案无代码,纯描述
-
BeanPostProcessor
和FactoryBean
职责分离,可以分为两个类 - 分离后
BeanPostProcessor
根据配置yml 定义的配置进行赋值 filter,避免其去加载其他bean 导致输出 - 分离后
FactoryBean
根据配置yml 定义的配置进行赋值filter
PS总结:通过将依赖的条件分离,且基于配置文件使用@Value
方式去读取,避免牵连加载其他bean 导致大量日志输出,该方案较于A,能完全保留shiro 功能(有点鸡肋,只需要规范好shiro filter 都基于FactoryBean
管理),但是相对于官方API实现会有较大变更
网友评论