前言

  • 在Spring帮我们管理bean后,编写一些工具类的时候需要从容器中拿到一些对象来做一些操作,比如字典缓存工具类,在没有找到字典缓存时,需要dao对象从数据库load一次,再次存入缓存中。此时需要在util工具类中拿到ioc容器中的dao对象。

原理

  • spring容器在加载的时候会把ApplicationContext注入到实现了ApplicationContextAware的类中,拿到applicationContext后,可以通过getBean来拿到ioc容器中管理的对象

  • 通过实现DisposableBean接口,在容器消亡时,清除注入的applicationContext.

代码

  1. package com.hyq.util;
  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. @Service
  9. @Lazy(false)
  10. /**
  11. * 获取bean的工具类(通过注入applicationCotnext)
  12. * @author hyq
  13. *
  14. */
  15. public class SpringUtils implements ApplicationContextAware,DisposableBean
  16. {
  17. public static ApplicationContext applicationContext = null;
  18. @SuppressWarnings("unchecked")
  19. public static <T> T getBean(String beanName){
  20. isInjected();
  21. return (T) applicationContext.getBean(beanName);
  22. }
  23. public static <T> T getBean(Class<T> requiredType){
  24. isInjected();
  25. return applicationContext.getBean(requiredType);
  26. }
  27. @Override
  28. public void destroy() throws Exception {
  29. System.out.println("springUtils工具类清除注入的applicationContext");
  30. SpringUtils.applicationContext = null;
  31. }
  32. @Override
  33. public void setApplicationContext(ApplicationContext applicationContext)
  34. throws BeansException {
  35. System.out.println("springUtils工具类注入的applicationContext");
  36. SpringUtils.applicationContext = applicationContext;
  37. }
  38. /**
  39. * 判断是否注入
  40. * @return
  41. */
  42. public static void isInjected(){
  43. if(SpringUtils.applicationContext == null){
  44. throw new RuntimeException("springUtils applicationContext is not injected!");
  45. }
  46. }
  47. public static void main(String[] args) {
  48. System.out.println(applicationContext);
  49. }
  50. }

可以随时拿取spring容器中Bean的工具类的更多相关文章

  1. 【sping揭秘】3、Spring容器中bean默认是保持一个实例

    Spring容器中bean默认是保持一个实例 这里做一个测试,基础代码 package cn.cutter.start.provider; import org.springframework.con ...

  2. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  3. Spring 容器中 Bean 的生命周期

    Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...

  4. (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  5. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)

    Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...

  6. IoC基础篇(一)--- Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  7. Spring框架中,在工具类或者普通Java类中调用service或dao

    spring注解的作用: 1.spring作用在类上的注解有@Component.@Responsity.@Service以及@Controller:而@Autowired和@Resource是用来修 ...

  8. SSM(Spring)中,在工具类中调用服务层的方法

    因为平时在调用service层时都是在controller中,有配置扫描注入,spring会根据配置自动注入所依赖的服务层. 但因我们写的工具类不属于controller层,所以当所写接口需要调用服务 ...

  9. Spring 容器中bean的加载过程

    bean 的加载过程大致可以分为以下几个步骤: 1.获取配置的资源文件 2.对获取到的xml资源文件进行解析 3.获取包装资源 4.解析处理包装之后的资源 5.加载 提取bean 并进行注册(添加到b ...

随机推荐

  1. HADOOP docker(三):HDFS高可用实验

      前言1.机器环境2.配置HA2.1 修改hdfs-site.xml2.2 设置core-site.xml3.配置手动HA3.1 关闭YARN.HDFS3.2 启动HDFS HA4.配置自动HA4. ...

  2. Linux下安装paramiko

    paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. 由于使用的是python这样的能够跨平台运行的语言,所以所有python支持的平台, ...

  3. POJ 2104 K-th Number(划分树)

    Description You are working for Macrohard company in data structures department. After failing your ...

  4. 第三章——供机器读取的数据(CSV与JSON)

    本书使用的文件.代码:https://github.com/huangtao36/data_wrangling 机器可读(machine readable)文件格式: 1.逗号分隔值(Comma-Se ...

  5. lintcode-170-旋转链表

    170-旋转链表 给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数 样例 给出链表1->2->3->4->5->null和k=2 返回4-> ...

  6. Web界面和Winform界面生成,代码生成工具

    在上面一篇随笔<代码生成工具之界面快速生成>介绍了代码生成工具Database2Sharp的界面生成操作,其中介绍了Web界面(包括列表界面.内容显示.内容编辑界面的生成,另外还介绍了Wi ...

  7. (转)Linux NUMA引发的性能问题

    最近某客户的核心业务系统又出了翻译缓慢的情况.该问题在6月份也出现过,当时进行了一次调整. 我们首先来看下故障时间段的awr报告: 单纯的从TOP 5 event,基本上是看不出任何东西的,可能有人会 ...

  8. 再看perf是如何通过dwarf处理栈帧的

    从结构体stack_dump入手, util/unwind-libunwind-local.c 中有函数access_mem #0 access_mem (as=0x1f65bd0, addr=140 ...

  9. Microsoft Edge goes Chromium

    Microsoft Edge goes Chromium https://techcrunch.com/2018/12/06/microsoft-edge-goes-chromium-and-maco ...

  10. [计算机网络-传输层] 无连接传输:UDP

    UDP(用户数据报协议) 下面是UDP的报文段格式: 可以看出UDP的首部长度是固定的,共64bit,即8个字节. 校验和:提供了差错检测得功能,即用于确定当UDP报文段从源到达目的时,其中的比特是否 ...