spring中InitializingBean的使用

1、InitializingBean接口

InitializingBean接口中只包含一个afterPropertiesSet()方法,继承该接口的类,在初始化bean的时候都会执行该方法,且只执行一次
测试如下

package com.rookie.bigdata.Initializingbean;

import org.springframework.beans.factory.InitializingBean;

/**
 * @author
 * @date 2019/5/22
 */
public class AfterInitializingBean implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {

        System.out.println("执行afterPropertiesSet()方法");
    }

    public void init() {
        System.out.println("执行了init的方法");
    }
}

application-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">


    <bean id="AfterInitializingBean" class="com.rookie.bigdata.Initializingbean.AfterInitializingBean" init-method="init"></bean>


</beans>

测试类及测试结果

    @Test
    public void afterPropertiesSet() throws Exception {

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/rookie/bigdata/Initializingbean/application-bean.xml");
    }


在spring初始化bean的时候,如果该bean是实现了InitializingBean接口,并且同时在配置文件中指定了init-method,系统则是先调用afterPropertiesSet方法,然后在调用init-method中指定的方法

2、AbstractAutowireCapableBeanFactory

出现上面结果的原因为在加载bean的时候执行了AbstractAutowireCapableBeanFactory类中的invokeInitMethods()方法

protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
			throws Throwable {

			//判断给bean对象是否实现了InitializingBean,如果实现了该接口,就调用afterPropertiesSet方法
		boolean isInitializingBean = (bean instanceof InitializingBean);
		if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
			if (logger.isTraceEnabled()) {
				logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
			}
			if (System.getSecurityManager() != null) {
				try {
					AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
			//调用afterPropertiesSet方法					
						((InitializingBean) bean).afterPropertiesSet();
						return null;
					}, getAccessControlContext());
				}
				catch (PrivilegedActionException pae) {
					throw pae.getException();
				}
			}
			else {
			//调用afterPropertiesSet方法
				((InitializingBean) bean).afterPropertiesSet();
			}
		}

		if (mbd != null && bean.getClass() != NullBean.class) {
			String initMethodName = mbd.getInitMethodName();
			//判断是否指定了init-method方法,如果指定了init-method方法,则再调用制定的init-method
			if (StringUtils.hasLength(initMethodName) &&
					!(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
					!mbd.isExternallyManagedInitMethod(initMethodName)) {
				invokeCustomInitMethod(beanName, bean, mbd);
			}
		}
	}

3、总结

1、spring执行的时候,首先会调用afterPropertiesSet方法,其次当配置文件中配置了init-method,会调用init-method指定的方法
2、spring执行的时候,首先会调用afterPropertiesSet方法,当配置文件中配置了init-method,且方法为afterPropertiesSet时,该方法只调用一次

posted on 2022-03-23 11:38  海之浪子  阅读(301)  评论(0编辑  收藏  举报

导航