package com.geostar.geostack.git_branch_manager.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* @author 王睿
* @date 2019-01-16 16:02
*/
@Component
public class SpringUtils implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringUtils.applicationContext == null) {
SpringUtils.applicationContext = applicationContext;
}
} //获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
} //通过name获取 Bean.
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
} //通过class获取Bean.
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
} //通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}

via:https://www.cnblogs.com/s648667069/p/6489557.html

SpringBoot之普通类获取Spring容器中的bean的更多相关文章

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

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

  2. [十]SpringBoot 之 普通类获取Spring容器中的bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...

  3. Spring Boot中普通类获取Spring容器中的Bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...

  4. 获取Spring容器中的Bean协助调试

    在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试. 第一步:编写自定义监听器 /** * 监听serv ...

  5. 获取Spring容器中的Bean

    摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...

  6. 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)

    ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述:   即是说,当一个类实现了这个接口之 ...

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

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

  8. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  9. java web中如何获取spring容器中定义的bean----WebApplicationContext的使用

    本文简单编写一个servlet来获取spring容器中管理的<bean  id="dateBean" class="java.util.Date" sin ...

随机推荐

  1. Python全栈学习_作业集锦(持续更新)

    python基础 day1 python初识 . 计算机基础(cpu,内存,硬盘,操作系统) . Python出生于应用 . python发展史 . 编程语言分类 . python优缺点 . pyth ...

  2. 如何解决Dynamics 365的错误:用户身份验证无效,MSIS0006

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复246或者20170312可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  3. Unity Profiler的使用

    选中Development Build.Autoconnect Profiler和Script Debugging三个选项,如下图所示. 点击Build And Run按钮,将会编译项目并安装APK到 ...

  4. 微信小程序(一),授权页面搭建

    wxml代码如下: <!--pages/index2/index2.wxml--> <view class="index2Container"> <i ...

  5. 初级c++编码规范

        想了很久,第一篇文章还是应该写编码规范好一点.编码规范是一个仁者见仁的问题,为了避免复杂庞大,自己总结了一套简单版本的规范. 简介     本文介绍一份自己使用的C++编码规范.第一次正式进入 ...

  6. PostgreSql 查询表结构和说明

    select (select relname from pg_class where oid=a.attrelid) relname , () as comment from pg_class whe ...

  7. mysql免安装版初次使用

    在自己电脑上安装一个mysql数据库并启动,碰到一些问题,总结一下 1.下载免安装版mysql数据库,百度下载了了5.7.25版本 2.在bin文件夹下找到my-defaults.ini文件,我这没有 ...

  8. C++调用Opencv实践中遇到的问题备忘录

    1.编写一个显示图片的项目,但显示的图片全灰色. 原因:需要在imshow()函数前加一个namedWindow()函数.https://blog.csdn.net/mao_hui_fei/artic ...

  9. Vector与ArrayList区别

    1)Vector的方法都是同步的(Synchronized),是线程安全的: ArrayList的方法是线程不安全的. 由于线程同步必然会影响性能,因此,ArrayList的性能比Vector好. 请 ...

  10. 【Python 21】52周存钱挑战1.0

    1.案例描述 按照52周存钱法,存钱人必须在一年52周内,每周递存10元.例如,第一周存10元,第二周存20元,第三周存30元,直到第52周存520元. 记录52周后能存多少钱?即10+20+30+. ...