在spring容器初始化bean和销毁bean的以前的操作有很多种,
  目前我知道的有:在xml中定义的时候用init-method和destory-method,还有一种就是定义bean的时候实现DisposableBean和InitializingBean 这两个接口,打开InitializingBean 的源码:

  1. public interface InitializingBean {
  2.  
  3. /**
  4. * Invoked by a BeanFactory after it has set all bean properties supplied
  5. * (and satisfied BeanFactoryAware and ApplicationContextAware).
  6. * <p>This method allows the bean instance to perform initialization only
  7. * possible when all bean properties have been set and to throw an
  8. * exception in the event of misconfiguration.
  9. * @throws Exception in the event of misconfiguration (such
  10. * as failure to set an essential property) or if initialization fails.
  11. */
  12. void afterPropertiesSet() throws Exception;
  13.  
  14. }

根据注解很清楚的可以看出,afterPropertiesSet()表示在资源加载完以后,初始化bean之前执行的方法,我猜想spring底层应该会在初始化bean的时候,应该会使用(bean instanceof InitializingBean)判断是不是实现了这个接口,其实在很多框架中都是这么干的,但是因为没研究过spring源码,暂且还不知道底层原理。这样我们就可以在初始化的时候,做一些自己想要做的事了。
  同理,DisposableBean就是在一个bean被销毁的时候,spring容器会帮你自动执行这个方法,估计底层原理也是差不多的,对于一些使用完之后需要释放资源的bean,我们都会实现这个接口,或者是配置destory-method方法。源码也基本是相似的,只是把afterPropertiesSet改为destroy。

ApplicationContextAware
  其实我们看到---Aware就知道是干嘛用的了,就是属性注入的,但是这个ApplicationContextAware的不同地方在于,实现了这个接口的bean,当spring容器初始化的时候,会自动的将ApplicationContext注入进来:

  1. import org.apache.commons.lang.Validate;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.DisposableBean;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.ApplicationContextAware;
  6. import org.springframework.context.annotation.Lazy;
  7. import org.springframework.stereotype.Service;
  8. /**
  9. * applicationContext静态化
  10. * 使用了ApplicationContextAware接口的类,如果受spring容器管理的
  11. * 话,那么就会自动的调用ApplicationContextAware中的setApplicationContext方法
  12. * @author Hotusm
  13. *
  14. */
  15. @Service
  16. @Lazy(false)
  17. public class SpringContextHolder implements ApplicationContextAware,DisposableBean{
  18.  
  19. private static ApplicationContext applicationContext;
  20.  
  21. @Override
  22. public void setApplicationContext(ApplicationContext applicationContext)
  23. throws BeansException {
  24.  
  25. SpringContextHolder.applicationContext=applicationContext;
  26. }
  27. //清空applicationContext 设置其为null
  28. @Override
  29. public void destroy() throws Exception {
  30. SpringContextHolder.clearHolder();
  31. }
  32. //获得applicationContext
  33. public static ApplicationContext getApplicationContext() {
  34. //assertContextInjected();
  35. return applicationContext;
  36. }
  37.  
  38. public static void clearHolder(){
  39. applicationContext=null;
  40. }
  41. //获取Bean
  42. public static <T> T getBean(Class<T> requiredType){
  43. //assertContextInjected();
  44. return (T) getApplicationContext().getBean(requiredType);
  45. }
  46. @SuppressWarnings("unchecked")
  47. public static <T> T getBean(String name){
  48. assertContextInjected();
  49. return (T) getApplicationContext().getBean(name);
  50. }
  51. //判断application是否为空
  52. public static void assertContextInjected(){
  53. Validate.isTrue(applicationContext==null, "application未注入 ,请在springContext.xml中注入SpringHolder!");
  54. }
  55.  
  56. }

因为我们在做开发的时候,并不是说在每一个地方都能将属性注入到我们想要的地方去的,比如在Utils使用到dao,我们就不能直接注入了,这个时候就是我们需要封装springContext的时候了,而ApplicationContextAware就起了关键性的作用。

3:还有一种是注解的用法:

在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。

spring中的DisposableBean和InitializingBean,ApplicationContextAware的用法的更多相关文章

  1. Spring中@Resource与@Autowired、@Qualifier的用法与区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...

  2. Spring中@Resource与@Autowired、@Qualifier的用法与区别(转)

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...

  3. Spring中Bean的生命中期与InitializingBean和DisposableBean接口

    Spring提供了一些标志接口,用来改变BeanFactory中的bean的行为.它们包括InitializingBean和DisposableBean.实现这些接口将会导致BeanFactory调用 ...

  4. Spring常用工具类(ApplicationContextAware、DisposableBean、InitializingBean)

    原创作品,出自 "晓风残月xj" 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj). 由于各种原因,可能存在诸多不 ...

  5. Spring中的InitializingBean与DisposableBean

    InitializingBean顾名思义,应该是初始化Bean相关的接口. 先看一下该接口都定义了哪些方法: public interface InitializingBean { void afte ...

  6. spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入

    <spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...

  7. spring中ApplicationContextAware接口描述

    项目中使用了大量的工厂类,采用了简单工厂模式: 通过该工厂类可以获取指定的处理器bean,这些处理器bean我们是从spring容器中获取的,如何获取,是通过实现ApplicationContextA ...

  8. spring中InitializingBean接口使用理解

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...

  9. Spring中的InitializingBean接口的使用

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法. 测试,如下: imp ...

随机推荐

  1. MySQL中导入 导出CSV

    来自:http://blog.csdn.net/sara_yhl/article/details/6850107 导出 select * from test_info into outfile '/t ...

  2. 单源最短路径算法---Dijkstra

    Dijkstra算法树解决有向图G=(V,E)上带权的单源最短路径问题,但是要求所有边的权值非负. 解题思路: V表示有向图的所有顶点集合,S表示那么一些顶点结合,从源点s到该集合中的顶点的最终最短路 ...

  3. 微软MVC对架构的一点思考

    毕业即将三年,在学校学习.做毕设一直使用拖控件的 Winform\WebForm,工作后公司采用MVC3架构做项目. 下面使用mvc的个人总结 : 1.架构上分层清晰.便于研发,耦合性好 2.缓存机制 ...

  4. JSP Servlet性能分析

    JSP Servlet性能分析:http://www.docin.com/p-757790851.html

  5. asp.net首页设置

    在web.config中设置首页 <configuration> <system.web> <compilation debug="true" tar ...

  6. 关于IT概念的一些思考

    同事提及“软件工程.软件生命周期.项目管理.CMMI.IPD.RUP.UML及UML建模.面向对象分析与设计.需求分析.系统分析与设计……等等,它们到底是什么?它们之间有什么关系?”   下面是个人见 ...

  7. Apache配置多个网站的方法

    Apache的虚拟主机是一种允许在同一台机器上,运行超过一个网站的解决方案.虚拟主机有两种,一种叫基于IP的(IP-based),另一种叫基于名字的(name-based).虚拟主机的存在,对用户来说 ...

  8. 【MVC 过滤器的应用】ASP.NET MVC 如何统计 Action 方法的执行时间

    代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; u ...

  9. 事务复制中的snapshot

        Snapshot agent读取article的信息,将article的内容和脚本放置到snapshot文件夹中: 接下来distribution agent会读取这些快照文件,传输到订阅,完 ...

  10. ruby -- 进阶学习(一)subdomain配置与实现

    今天和guanMac童鞋研究的subdomain配置终于有点头绪~~ 之所以会遇到种种难题,个人总结了一下,第一本人太菜,第二英语不好 贴一下guanMac童鞋配置小结的链接:http://my.eo ...