栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

解决:is not eligible for getting processed by all BeanPostProcessors

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决:is not eligible for getting processed by all BeanPostProcessors

文章目录
    • 问题描述
    • 分析
    • 总结
    • 解决
      • 方案1:延迟初始化
      • 方案2:@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    • 参考

问题描述

SpringBoot项目整合Shiro框架后启动,启动日志里大概会看到下面的日志:

...
(org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker:376) - Bean 'userServiceImpl' of type [com.xxx.impl.UserServiceImpl] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
...
(org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker:376) - Bean 'jwtRealm' of type [com.xxx.JWTRealm] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
(org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker:376) - Bean 'securityManager' of type [org.apache.shiro.web.mgt.DefaultWebSecurityManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
(org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker:376) - Bean 'authorizationAttributeSourceAdvisor' of type [org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

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

中文描述:
xxx这个bean不能被所有的BeanPostProcessors处理(比如:自动代理(即:AOP))

这几个bean(称为业务bean)不会被所有BeanPostProcessor(BPP)处理。BPP是spring的内置的基础bean,用于在实例化业务bean前后处理一些逻辑,可以更改业务bean的行为甚至返回另一个bean(大名鼎鼎的AOP就是通过BPP实现)。

依赖了UserService的某个bean实现了BeanPostProcessor之类的接口,导致userServiceImpl被提前注入,导致无法被AOP。

分析

在项目中的ShiroConfig文件中使用了ShiroFilterFactoryBean,它也实现了BeanPostProcessor接口。

上面的错误信息,查看Spring源码,是在BeanPostProcessorChecker中定义的:

	
	private static final class BeanPostProcessorChecker implements BeanPostProcessor {
		...
	}
总结

出问题的原因:业务bean在BPP实例化前被实例化了。理想情况下应该让spring先实例化所有的BPP后,再实例化我们的业务bean,这样业务bean才能被所有BPP处理,如果有业务bean先于任何BPP实例化,那么这个业务bean就不会被还未实例化的BPP处理了,这个就是日志提示的原因。

解决 方案1:延迟初始化

可以使用延迟初始化来解决。延迟初始化会在IOC的时候先给出一个代理类,IOC之后的AOP这个BeanPostProcessor会正常地给它处理。

具体做法
加一个注解即可:@Lazy。可以在两个地方加这个注解:字段、方法参数。
方法1:加到字段上
方法2:加到构造方法的参数上

方案2:@Role(BeanDefinition.ROLE_INFRASTRUCTURE)

方案说明
在Bean的声明上加个注解:@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
表明这个bean是完全后台模式,不需要被代理。

原理,就是BeanPostProcessorChecker的下面这个方法:

@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;
}
参考

https://www.jianshu.com/p/b1209cd3686d

https://www.baeldung.com/spring-not-eligible-for-auto-proxying

https://stackoverflow.com/questions/1201726/tracking-down-cause-of-springs-not-eligible-for-auto-proxying

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930202.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号