1.bean的初始化操作
1.1通过在配置文件中,设置bean的init-method方法,destory-method方法,spring框架会通过反射机制来调用
java类
package com.crazycoder2010.spring.init; public class InitDestoryMethodBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } /** * 当属性被注入完毕后把注入的值打印出来 通常用来检测bean的各个必填属性是否被赋值 */ public void init() { if (name == null) { throw new IllegalArgumentException("name should not be null!!"); } System.out.println("InitMethodBean.init() was called,name=" + name); } /** * 容器销毁时执行 */ public void destory() { System.out.println("InitDestoryMethodBean.destory() was called."); } }
该bean的配置
<bean id="initMethodBean" class="com.crazycoder2010.spring.init.InitDestoryMethodBean" init-method="init" destroy-method="destory"><!-- 这个init-method和destory-method属性是关键 --> <property name="name" value="Kevin" /> </bean>
方法调用
public static void main(String[] args) { InitDestoryMethodBeanTest test = new InitDestoryMethodBeanTest(); test.getBean("initMethodBean"); }
输出
InitMethodBean.init() was called,name=Kevin
1.2实现org.springframework.beans.factory.InitializingBean接口
java类
public class InitDestoryInterfaceBean implements InitializingBean,DisposableBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void afterPropertiesSet() throws Exception { if (name == null) { throw new IllegalArgumentException("name should not be null!!"); } System.out.println("InitMethodBean.init() was called,name=" + name); } @Override public void destroy() throws Exception { System.out.println("InitDestoryInterfaceBean.destroy() was called."); } }
bean配置-和普通的bean同样处理
<bean id="initInterfaceBean" class="com.crazycoder2010.spring.init.InitInterfaceBean"> <property name="name" value="Scott" /> </bean>
方法调用
public class InitDestoryInterfaceBeanTest extends BaseConfigurableSpring { public static void main(String[] args){ InitDestoryInterfaceBeanTest test = new InitDestoryInterfaceBeanTest(); test.getBean("initInterfaceBean"); test.beanFactory.destroySingletons(); } }
打印输出
InitMethodBean.init() was called,name=Scott
比较两种初始化方法
两种方式最大的区别在于第二中方式依赖spring框架本身,如果后期需要将spring地换成另一个ioc框架时需要作很大的改动,而第一中方式是不需要的,但是缺点也很明显,这种检查关系不明显,需要在配置文件中显示的去声明,然后才能知道这个方法的用途,其实笔者还是推荐第二中做法,因为实际应用中一旦用了spring,几乎不会去来回换框架折腾的:)
2.bean销毁
这个销毁和初始化类似,但是也有些差别,刚开始写了个destory方法,然后调用,发现怎么也不执行,后来看了下,必须显式的调用以下beanfactory.destroySingletons()方法才能执行:(
重新设计一下我们的基类,因为destorySingletons()方法是在DefaultSingletonBeanRegistry类中定义的
package com.crazycoder2010.spring.init; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import com.crazycoder2010.spring.BaseSpring; public class BaseConfigurableSpring extends BaseSpring { XmlBeanFactory beanFactory ;//这里换成了XmlBeanFactory public Object getBean(String beanName) { init();//确保spring容器已经启动 return beanFactory.getBean(beanName); } private void init() { if (beanFactory == null) { beanFactory = new XmlBeanFactory(new ClassPathResource(configFiles()[0])); } } }
bean的配置和init类似,第一种方案添加destory-method属性,指向销毁的方法,第二种做法是实现org.springframework.beans.factory.InitializingBean接口