首页 技术 正文
技术 2022年11月12日
0 收藏 807 点赞 3,355 浏览 2946 个字

关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:

第一种:通过注解@PostConstruct  和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

  1. import javax.annotation.PostConstruct;
  2. import javax.annotation.PreDestroy;
  3. public class DataInitializer{
  4. @PostConstruct
  5. public void initMethod() throws Exception {
  6. System.out.println(“initMethod 被执行”);
  7. }
  8. @PreDestroy
  9. public void destroyMethod() throws Exception {
  10. System.out.println(“destroyMethod 被执行”);
  11. }
  12. }

Spring中初始化bean和销毁bean的时候执行某个方法的详解

第二种是:通过 在xml中定义init-method 和  destory-method方法

  1. public class DataInitializer{
  2. public void initMethod() throws Exception {
  3. System.out.println(“initMethod 被执行”);
  4. }
  5. public void destroyMethod() throws Exception {
  6. System.out.println(“destroyMethod 被执行”);
  7. }
  8. }

Spring中初始化bean和销毁bean的时候执行某个方法的详解

  1. <bean id=”dataInitializer” class=”com.somnus.demo.DataInitializer” init-method=”initMethod” destory-method=”destroyMethod”/>

Spring中初始化bean和销毁bean的时候执行某个方法的详解

第三种是:通过bean实现InitializingBean和 DisposableBean接口

  1. import org.springframework.beans.factory.DisposableBean;
  2. public class DataInitializer implements InitializingBean,DisposableBean{
  3. @Override
  4. public void afterPropertiesSet() throws Exception {
  5. System.out.println(“afterPropertiesSet 被执行”);
  6. }
  7. @Override
  8. public void destroy() throws Exception {
  9. System.out.println(“destroy 被执行”);
  10. }
  11. }

Spring中初始化bean和销毁bean的时候执行某个方法的详解

其中第一种和第二种是同一种形式,只不过一种xml配置,另外一种采用注解形式罢了,有很大区别的是第三种,

如果同一个bean同时采用两种方式初始化的时候执行某个方法,首先在执行顺序上就会体现出来。

先执行afterPropertiesSet(),

后执行initMethod()

这里我们看下源码

这方式在spring中是怎么实现的?

通过查看spring的加载bean的源码类(AbstractAutowireCapableBeanFactory)可看出其中奥妙

AbstractAutowireCapableBeanFactory类中的invokeInitMethods讲解的非常清楚,源码如下:

  1. protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
  2. throws Throwable {
  3. //判断该bean是否实现了实现了InitializingBean接口,如果实现了InitializingBean接口,则只掉调用bean的afterPropertiesSet方法
  4. boolean isInitializingBean = (bean instanceof InitializingBean);
  5. if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod(“afterPropertiesSet”))) {
  6. if (logger.isDebugEnabled()) {
  7. logger.debug(“Invoking afterPropertiesSet() on bean with name ‘” + beanName + “‘”);
  8. }
  9. if (System.getSecurityManager() != null) {
  10. try {
  11. AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
  12. public Object run() throws Exception {
  13. //直接调用afterPropertiesSet
  14. ((InitializingBean) bean).afterPropertiesSet();
  15. return null;
  16. }
  17. },getAccessControlContext());
  18. } catch (PrivilegedActionException pae) {
  19. throw pae.getException();
  20. }
  21. }
  22. else {
  23. //直接调用afterPropertiesSet
  24. ((InitializingBean) bean).afterPropertiesSet();
  25. }
  26. }
  27. if (mbd != null) {
  28. String initMethodName = mbd.getInitMethodName();
  29. //判断是否指定了init-method方法,如果指定了init-method方法,则再调用制定的init-method
  30. if (initMethodName != null && !(isInitializingBean && “afterPropertiesSet”.equals(initMethodName)) &&
  31. !mbd.isExternallyManagedInitMethod(initMethodName)) {
  32. //进一步查看该方法的源码,可以发现init-method方法中指定的方法是通过反射实现
  33. invokeCustomInitMethod(beanName, bean, mbd);
  34. }
  35. }

Spring中初始化bean和销毁bean的时候执行某个方法的详解

总结:

1:spring为bean提供了两种初始化bean的方式,实现InitializingBean接口,实现afterPropertiesSet方法,或者在配置文件中同过

init-method指定,两种方式可以同时使用

2:实现InitializingBean接口是直接调用afterPropertiesSet方法,比通过反射调用init-method指定的方法效率相对来说要高点。但是

init-method方式消除了对spring的依赖

3:如果调用afterPropertiesSet方法时出错,则不调用init-method指定的方法。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905