Java技能积累-bean属性初始化后执行某个方法

张开发
2026/4/12 22:13:18 15 分钟阅读

分享文章

Java技能积累-bean属性初始化后执行某个方法
文章目录一、InitializingBean一、InitializingBeanInitializingBean 是 Spring 提供的拓展性接口它为 bean 提供了属性初始化后的处理方法。该接口只有一个名为 afterPropertiesSet 的方法凡是继承该接口的类在 bean 的属性初始化后都会执行该方法。在 Spring 初始化 bean 时如果 bean 实现了 InitializingBean 接口会自动调用 afterPropertiesSet 方法。/** * Interface to be implemented by beans that need to react once all their properties * have been set by a {link BeanFactory}: e.g. to perform custom initialization, * or merely to check that all mandatory properties have been set. * * pAn alternative to implementing {code InitializingBean} is specifying a custom * init method, for example in an XML bean definition. For a list of all bean * lifecycle methods, see the {link BeanFactory BeanFactory javadocs}. * * author Rod Johnson * author Juergen Hoeller * see DisposableBean * see org.springframework.beans.factory.config.BeanDefinition#getPropertyValues() * see org.springframework.beans.factory.support.AbstractBeanDefinition#getInitMethodName() */publicinterfaceInitializingBean{/** * Invoked by the containing {code BeanFactory} after it has set all bean properties * and satisfied {link BeanFactoryAware}, {code ApplicationContextAware} etc. * pThis method allows the bean instance to perform validation of its overall * configuration and final initialization when all bean properties have been set. * throws Exception in the event of misconfiguration (such as failure to set an * essential property) or if initialization fails for any other reason */voidafterPropertiesSet()throwsException;}

更多文章