BeanPostProcessor怎么在spring中的应用
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,这篇文章主要介绍了BeanPostProcessor怎么在spring中的应用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇BeanPostProcessor怎么在spr
千家信息网最后更新 2025年02月02日BeanPostProcessor怎么在spring中的应用1)断点查看代码
1.在MyBeanPostProcessor添加断点,运行测试方法通过观察栈,查看执行流程 下方长长的英文是调试中右下角的窗口: 2. :84, AnnotationConfigApplicationContext 初始化容器
3. refresh:543, AbstractApplicationContext 调用refresh() 函数 ,实例化所有剩余的单例
4 .finishBeanFactoryInitialization:867, AbstractApplicationContext
preInstantiateSingletons:761, DefaultListableBeanFactory,具体通过getBean函数进行初始化剩余的Bean 5. doGetBean:302, AbstractBeanFactory 这一步 通过getSingleton 获取单实例, 通过初始化createBean 方法创建Bean 6. createBean:483, AbstractAutowireCapableBeanFactory,调用初始化,获取Bean 实例
7. doCreateBean:555, AbstractAutowireCapableBeanFactory 进入到具体创建Bean 中 通过populateBean() 进行填充Bean 操作 8.最后可以看到 initializeBean:1620, AbstractAutowireCapableBeanFactory 中 执行 applyBeanPostProcessorsBeforeInitialization 和 applyBeanPostProcessorsAfterInitialization 进行 postProcessBeforeInitialization() 函数和postProcessAfterInitialization() 函数的调用;中间调用 invokeInitMethods(beanName, wrappedBean, mbd) ;进行 初始化 函数的调用 9. applyBeanPostProcessorsBeforeInitialization:409,AbstractAutowireCapableBeanFactory 最后通过 getBeanPostProcessors() 扫描所有的 BeanPostProcessor 直到遇到null 进行返回退出循环 2) 总结 运行过程中的执行原理
通过查看断点调试结果, 可以看到,创建单例容器Bean 通过 在spring 中大量使用BeanPostProcessor ,进行处理,下面举例3个进行断点、代码测试: 1).ApplicationContextAwareProcessor 帮助组件注入IOC 容器 在Dog 中实现 implements ApplicationContextAware 接口,编写, setApplicationContext 将容器保存至属性方法中 1. postProcessBeforeInitialization:97,ApplicationContextAwareProcessor 可以看到 通过 postProcessBeforeInitialization 函数进行将容器进行赋值 2).InitDestroyAnnotationBeanPostProcessor 用于处理 @PostConstruct 、@PreDestroy 注解。 查看 InitDestroyAnnotationBeanPostProcessor 源码, postProcessBeforeInitialization 函数代码,如下: 使用 findLifecycleMetadata(bean.getClass()); 获取所有的生命周期函数,通过 metadata.invokeInitMethods(bean, beanName); 进行代理调用对应的生命周期函数 3).BeanValidationPostProcessor 数据校验 用于进行数据校验,查看 BeanValidationPostProcessor 的源码 postProcessBeforeInitialization 和 postProcessAfterInitialization 代码如下,进行 doValidate()函数进行数据校验。
这篇文章主要介绍了BeanPostProcessor怎么在spring中的应用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇BeanPostProcessor怎么在spring中的应用文章都会有所收获,下面我们一起来看看吧。
1.BeanPostProcessor 执行原理
public AnnotationConfigApplicationContext(Class>... annotatedClasses) { this(); register(annotatedClasses); refresh(); }
// Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory);
// Instantiate all remaining (non-lazy-init) singletons. beanFactory.preInstantiateSingletons();
getBean(beanName);
// Create bean instance. if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, new ObjectFactory
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
Object exposedObject = bean; try { populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { exposedObject = initializeBean(beanName, exposedObject, mbd); } }
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
result = beanProcessor.postProcessBeforeInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}
populateBean() 函数进行填充,invokeInitMethods(beanName, wrappedBean, mbd);
函数进行初始化,然后前后有applyBeanPostProcessorsBeforeInitialization 和applyBeanPostProcessorsAfterInitialization 进行对所有的BeanPostProcessor 扫描执行。
2.BeanPostProcessor 在spring 中的应用
@Component
public class Dog implements ApplicationContextAware {
private ApplicationContext applicationContext;
public Dog(){
System.out.println("dog constructor...");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
this.applicationContext = applicationContext;
}
}
2.通过invokeAwareInterfaces判断Bean 的类型进行类型转换,并进行赋值
private void invokeAwareInterfaces(Object bean) { if (bean instanceof Aware) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware) bean).setMessageSource(this.applicationContext); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); } } }
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass()); try { metadata.invokeInitMethods(bean, beanName); } catch (InvocationTargetException ex) { throw new BeanCreationException(beanName, "Invocation of init method failed", ex.getTargetException()); } catch (Throwable ex) { throw new BeanCreationException(beanName, "Failed to invoke init method", ex); } return bean; }
public void invokeInitMethods(Object target, String beanName) throws Throwable { CollectioninitMethodsToIterate = (this.checkedInitMethods != null ? this.checkedInitMethods : this.initMethods); if (!initMethodsToIterate.isEmpty()) { boolean debug = logger.isDebugEnabled(); for (LifecycleElement element : initMethodsToIterate) { if (debug) { logger.debug("Invoking init method on bean '" + beanName + "': " + element.getMethod()); } element.invoke(target); } }}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (!this.afterInitialization) {
doValidate(bean);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (this.afterInitialization) {
doValidate(bean);
}
return bean;
}
关于"BeanPostProcessor怎么在spring中的应用"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"BeanPostProcessor怎么在spring中的应用"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
函数
应用
容器
代码
断点
实例
数据
方法
知识
内容
原理
周期
周期函数
源码
生命
篇文章
类型
剩余
处理
测试
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
什么是应用计算机网络技术
云服务器ecs环境配置
时空猎人的服务器是通的吗
必选排斥关系数据库实现
搭建服务器免流教程
游戏服务器总回弹
mugen服务器
塔里木大学网络安全人才培养
服务器充电宝安全吗
dm数据库去除创建表的引号
帝国 数据库 模板
为何手机显示不在服务器内
度斯数据库
服务器失败联系管理员
服务器命令大全
青少年网络安全家长意见建议
中兴软件开发岗面试
3 emerald数据库
自动化硕士面试软件开发
怎样检查单位网络安全
网络安全技术与应用 论文
三级网络技术激活码
创建仓库数据库
游戏服务器总回弹
公安视频网网络安全
南京ctf网络安全大赛
第五人格内测服务器里珍贵的截图
常州网络软件开发供应商
海南省云服务器云空间
fifa21无法登陆服务器