在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题。为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean。因此就写了这个工具类,在此记录一下,方便后续查阅。废话不多说,直接上代码。

一、代码

package com.zxy.demo.spring;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;

/**
* 获取Spring容器中bean工具类

* @author liuysh
* @date 2018年6月6日
*
*/

@Component("springContextUtils")

public class SpringContextUtils implements ApplicationContextAware {

private static ApplicationContext applicationContext = null;

public static ApplicationContext getApplicationContext() {
return applicationContext;

}

@SuppressWarnings("unchecked")

public static <T> T getBean(String beanId) {

return (T) applicationContext.getBean(beanId);

}

public static <T> T getBean(Class<T> requiredType) {

return (T) applicationContext.getBean(requiredType);

}

/**

* Spring容器启动后,会把 applicationContext 给自动注入进来,然后我们把 applicationContext 赋值到静态变量中,方便后续拿到容器对象

* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)

*/

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

SpringContextUtils.applicationContext = applicationContext;

}
}

二、用法

        直接在代码用调用SpringContextUtils静态方法获取bean就ok,可以通过bean的id获取,也可以通过bean的类型获取
  1. RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class);
  2.  

三、注意事项

        仔细看代码可以发现我这个工具类上面加了个@Component注解。加这个注解的目的是为了把的SpringContextUtils的实例化交给Spring容器管理。当然,你也可以在xml中配置这个,我为了方便,直接用的注解。注意点:这个工具类的实例化必须由Spring容器来做

获取Spring容器Bean对象工具类的更多相关文章

  1. springboot自定义filter获取spring容器bean对象

    今天在自己定义的filter中,想要直接注入spring容器的其它bean进行操作,发现不能正常的注入: 原因:web容器加载顺序导致, 加载顺序是listener——filter——servlet, ...

  2. 普通java类获取spring容器bean的方法

    很多时候,我们在普通的java类中需要获取spring的bean来做操作,比如,在线程中,我们需要操作数据库,直接通过spring的bean中构建的service就可以完成.无需自己写链接..有时候有 ...

  3. web项目中获取spring的bean对象

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...

  4. 获取Spring容器Bean

    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); ManageResolver mr = (Ma ...

  5. Spring注解@Resource和@Autowired区别对比、spring扫描的默认bean的Id、程序获取spring容器对象

    -------------------------注解扫面的bean的ID问题-------------------------- 0.前提需要明白注解扫描出来的bean的id默认是类名首字母小写,当 ...

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

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

  7. 普通Java类获取spring 容器的bean的5种方法

    方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSu ...

  8. 【Spring】手动获取spring容器对象时,报no qualifying bean of type is defined

    手动获取容器对象时,报no qualifying bean of type is defined, 经过调查,发现手动获取的时候,该类所在的包必须经过spring容器初始化. 1.SpringConf ...

  9. SpringBoot 之 普通类获取Spring容器中的bean

    [十]SpringBoot 之 普通类获取Spring容器中的bean   我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...

随机推荐

  1. C#序列化与反序列化。

    序列化介绍: 把对象用一种新的格式来表示. 系列化只序列化数据. 序列化不建议使用自动属性 为什么要序列化: 将一个复杂的对象转换流,方便存储与信息交换. class Program { static ...

  2. SSLTLS 服务器瞬时 Diffie-Hellman 公共密钥过弱【原理扫描】解决说明

    一.  修改SSL密码套件 1.1  加固方法: 1.1.1  操作步骤: 第一步:按下' Win + R',进入"运行",键入" gpedit.msc",打开 ...

  3. vs 调试 iis中的网站

    打开网站,在vs中附加进程,选择w3wp.exe,如果不能下断点,设置一下pdb文件位置

  4. 建立uboot,内核的SI工程(1)

    1. 建立Uboot的SI工程1.1首先给uboot打上补丁,然后来生成压缩文件 tar cjf u-boot- 1.2 编译uboot make 100ask24x0_config //使用打好补丁 ...

  5. learnVUE-note

    title: learnVUE-note date: 2018-02-27 15:57:37 tags: categories: 前端技术 --- 本文是自己在学习Vue中的 VUE事件处理 在事件处 ...

  6. FE 命令随笔

    FE_CMD ————— * >>>>>>>> Vue ________________________________________________ ...

  7. Powershell中显示隐藏文件

    PS> Get-ChildItem -Path $home -Force PS> Get-ChildItem -Path $home -Hidden

  8. html/css更改子级继承的父级属性

    一个精美的网页需要的样式很多,在父级上设置的字体颜色或者大小,在其子元素中不一定全部相同,这时候要更改其中某一项的样式怎么办呢. 很多新手朋友就不明白,会迷惑为什么我使用class单独命名了,重新设置 ...

  9. IDEA 代码格式化,快捷键

    一键格式化代碼: Ctrl+Alt+L 全局搜索替换:ctrl+shift+r 强大的搜索功能,shift+shift (无论您想要搜啥都能找到) ctrl+shift+R==搜索类   CTRL+N ...

  10. react 的双向数据绑定

    学习过angular和vue的人都知道,它俩在实现双向数据绑定都是有一个专门的内置指令ngModel和v-model 但是在react中没有这些. 所以我们在react中想要实现双向数据绑定要调用一个 ...