<xml version="1.0" encoding="UTF-8"?>
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="lifeBean" class="research.spring.beanfactory.ch4.LifeCycleBean" init-method="init">
<bean>
<beans>
当Spring实例化lifeBean时,你会在控制台上看到” LifeCycleBean.init...”。
Spring要求init-method是一个无参数的方法,如果init-method指定的方法中有参数,那么Spring将会抛出java.lang.NoSuchMethodException
init-method指定的方法可以是public、protected以及private的,并且方法也可以是final的。
init-method指定的方法可以是声明为抛出异常的,就像这样:
final protected void init() throws Exception{
System.out.println("init method...");
if(true) throw new Exception("init exception");
}
如果在init-method方法中抛出了异常,那么Spring将中止这个Bean的后续处理,并且抛出一个org.springframework.beans.factory.BeanCreationException异常。
InitializingBean和init-method可以一起使用,Spring会先处理InitializingBean再处理init-method。
org.springframework.beans.factory.support. AbstractAutowireCapableBeanFactory完成一个Bean初始化方法的调用工作。 AbstractAutowireCapableBeanFactory是XmlBeanFactory的超类,再 AbstractAutowireCapableBeanFactory的invokeInitMethods方法中实现调用一个Bean初始化方法:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java:
//……
//在一个bean的合作者设备完成后,执行一个bean的初始化方法。
protected void invokeInitMethods(String beanName, Object bean, RootBeanDefinition mergedBeanDefinition) throws Throwable {
//判断bean是否实现了InitializingBean接口
if (bean instanceof InitializingBean) {
if (logger.isDebugEnabled()) {
logger.debug("Invoking afterPropertiesSet() on bean with name ‘" + beanName + "‘");
}
//调用afterPropertiesSet方法
((InitializingBean) bean).afterPropertiesSet();
}
//判断bean是否定义了init-method
if(mergedBeanDefinition!=null&&mergedBeanDefinition.getInitMethodName() != null) {
//调用invokeCustomInitMethod方法来执行init-method定义的方法
invokeCustomInitMethod(beanName, bean, mergedBeanDefinition.getInitMethodName());
}
}
//执行一个bean定义的init-method方法
protected void invokeCustomInitMethod(String beanName, Object bean, String initMethodName)
throws Throwable {
if (logger.isDebugEnabled()) {
logger.debug("Invoking custom init method ‘" + initMethodName +
"‘ on bean with name ‘" + beanName + "‘");
}
//使用方法名,反射Method对象
Method initMethod = BeanUtils.findMethod(bean.getClass(), initMethodName, null);
if (initMethod == null) {
throw new NoSuchMethodException("Couldn‘t find an init method named ‘" + initMethodName + "‘ on bean with name ‘" + beanName + "‘");
}
//判断方法是否是public
if (!Modifier.isPublic(initMethod.getModifiers())) {
//设置accessible为true,可以访问private方法。
initMethod.setAccessible(true);
}
try {
//反射执行这个方法
initMethod.invoke(bean, (Object[]) null);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
//………..
通过分析上面的源代码我们可以看到,init-method是通过反射执行的,而afterPropertiesSet是直接执行的。所以 afterPropertiesSet的执行效率比init-method要高,不过init-method消除了bean对Spring依赖。在实际使用时我推荐使用init-method。
需要注意的是Spring总是先处理bean定义的InitializingBean,然后才处理init-method。如果在Spirng处理InitializingBean时出错,那么Spring将直接抛出异常,不会再继续处理init-method。
如果一个bean被定义为非单例的,那么afterPropertiesSet和init-method在bean的每一个实例被创建时都会执行。单例 bean的afterPropertiesSet和init-method只在bean第一次被实例时调用一次。大多数情况下 afterPropertiesSet和init-method都应用在单例的bean上。
相关推荐
NULL 博文链接:https://yuui89828923aaa.iteye.com/blog/812569
3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...
EWA讲义 Hacettepe大学企业Web体系结构讲座的幻灯片和示例应用程序 应用清单: AjaxApp:基于Eclipse的Web项目,使用jQuery演示...spring-bean-lifecycle:使用init-method,destroy-method,InitializingBean,Disp
(6)如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法postProcessBeforeInitialzation()对Bean进行加工操作,这个非常重要,Spring的AOP就是用它实现的。 (7)如果Bean实现了...
> Spring bean factory 负责管理在 spring 容器中被创建的 bean 的生命周期。Bean 的生命 周期由两组回调(call back)方法组成。 > 初始化之后调用的回调方法。 > 销毁之前调用的回调方法。 > Spring 框架提供了...
同时,Spring还提供了扩展点(如BeanPostProcessor和InitializingBean接口),允许开发人员在Bean生命周期的不同阶段进行自定义操作。 AOP(面向切面编程)功能:Spring框架提供了AOP功能,通过切面(Aspect)来...
将会逐步扩大完善Spring Boot版本:2.1.7,兼容的Spring Cloud版本为Greenwich,版本对照参考概要如何配置拦截器:拦截器,过滤器,@ RestControllerAdvice bean的初始化:InitializingBean接口,@ conditionXXX注解...
NULL 博文链接:https://latex.iteye.com/blog/900664
1. Bean的定义,给出配置元数据,可通过xml、java类、注解三种方式 2. Bean的初始化,调用InitializingBean 接口定义的after
bean),即使用普通的“new”运算符创建 bean,然后 spring 进一步控制初始化 bean,替换 @Value 占位符 bean 属性上的值,调用 afterPropertiesSet InitializingBean 类型,注入@Autowired 或@Inject 属性等。...
主要介绍了Spring中的InitializingBean接口的使用,需要的朋友可以参考下
spring配置对象实例化后执行的方法,两种实现比对,通过InitializingBean接口,并实现arterPropertiesSet()方法;或者通过applicationContext.xml配置init-method属性
spring+InitializingBean+quartz+定时器-附件资源
All Classes ...InitializingBean InputStreamEditor InputStreamResource InputStreamSource InputTag InstantiationAwareBeanPostProcessor InstantiationAwareBeanPostProcessorAdapter ...
顺序:@PostConstruct> InitializingBean#afterPropertiesSet方法> initMethod Bean的预设初始化 延迟初始化和非延迟对象的差异:应用之上启动前后BeanInitialDemo Bean的销毁@PreDestroy> Dispo
.1. netty-sekiro 原文 感谢作者。以下是对原作者内容的学习提取 ...服务器端是用Springboot管理各种组件,在Springboot bean(InitializingBean)初始化 afterPropertiesSet完成会启动两个nio服务,分别监听5600
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework....