import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* 获取Spring上下文
*
* @author kelin.ll
* @date on 2019/7/18
*/
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} /**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
} /**
* 通过name获取 Bean.
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
} /**
* 通过class获取Bean.
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
} /**
* 通过name,以及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}

测试用例:

import com.provider.ServiceProviderApplication;import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* @author kelin.ll
* @date on 2019/7/18
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ServiceProviderApplication.class)
@Slf4j
public class ApplicationContextProviderTest {
@Test
public void getBeanByNameTest(){
TaskService taskService = (TaskService)ApplicationContextProvider.getBean("taskService");
System.out.println(taskService.getAll());
} @Test
public void getBeanByClassTest(){
TaskService taskService = ApplicationContextProvider.getBean(TaskService.class);
System.out.println(taskService.getAll());
} @Test
public void getBeanByNameAndClassTest(){
TaskService taskService = ApplicationContextProvider.getBean("taskService",TaskService.class);
System.out.println(taskService.getAll());
}
}

SpringBoot 上下文获取注入的Bean的更多相关文章

  1. java 从spring容器中获取注入的bean对象

      java 从spring容器中获取注入的bean对象 CreateTime--2018年6月1日10点22分 Author:Marydon 1.使用场景 控制层调用业务层时,控制层需要拿到业务层在 ...

  2. SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式

    前言: spring容器以xml的形式注入bean,然后可以在类中获取,获取的形式主要有二种:第一种最简单--采用@Resource 或@Autowired关键字在加载spring文件时将bean注入 ...

  3. springboot中如果使用了@Autowired注入了bean,则这个类也要为spring bean,new出来注入的bean为null

    https://blog.csdn.net/Mr_Runner/article/details/83684088 问题:new出来的实例中含有@Autowired注入时,注入的Bean为null: 解 ...

  4. SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean

    分类: [java]2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路 ...

  5. SpringBoot中获取上下文

    在实际开发中,有时候会根据某个bean的名称或class到Spring容器中获取对应的Bean.这里只做个简单的记录,方便后续自查. @Component public class SpringCon ...

  6. SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)

    先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...

  7. springboot的依赖注入报null的问题

    最近使用springboot开发项目,使用到了依赖注入,频繁的碰到注入的对象报空指针,错误如下 java.lang.NullPointerException: null at com.mayihc.a ...

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

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

  9. 【SpringBoot源码分析】-Bean的加载过程

    -- 以下内容均基于2.1.8.RELEASE版本 在<SpringBoot启动过程的分析>系列文章中简要的对SpringBoot整体的启动流程作了梳理,但并未针对诸多细节进行分析.前面的 ...

随机推荐

  1. H3C 802.11 WEP加密特点与注意事项

  2. zabbix--邮件告警报错“Support for SMTP authentication was not compiled in”

    zabbix 邮件告警报错“Support for SMTP authentication was not compiled in” 邮件报警失败:Support for SMTP authentic ...

  3. MySQL定义char和varchar类型,utf8编码,则最大值为多少?

    有道面试题:若一张表中只有一个字段VARCHAR(N)类型,utf8编码,则N最大值为多少?先明白计算的一些规则限制 4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字时,只 ...

  4. 34、使用Python操作MySql数据库和MsSql数据库

    一.数据库的安装和连接 1. PyMySQL的安装 pip install pymysql 2 .python连接数据库 import pymysql db = pymysql.connect(&qu ...

  5. react navite 学习资料

    react 学习资料 https://github.com/crazycodeboy/GitHubPopular crazycodeboy/GitHubPopular https://github.c ...

  6. 使用apache 的FileUtils处理文件的复制等操作

    今日思语:春风很柔,夏风很烈,秋风清爽,东风凛冽,愿你就是春夏秋冬的风~ 平时对一些文件进行操作,比如说写文件,读文件,复制一个文件等,使用原生File操作需要读取源文件,生成流对象,再写入一个新的文 ...

  7. CF1102D-Balanced Ternary String-(贪心)

    http://codeforces.com/problemset/problem/1102/D 题意: 有n个字符,只能为012,现要通过变换让012的数量相等,并且使字典序最小. 解题: 由样例可以 ...

  8. linux学习16 Linux用户和组管理命令演练和实战应用

    一.上集回顾 1.bash globing,IO重定向及管道 glob:*,?,[],[^] IO重定向: >,>>, 2>,2>> &>,& ...

  9. python函数 | 三元运算

    三元运算符就是在赋值变量的时候,可以直接加判断,然后赋值 格式: [on_true] if [expression] else [on_false] 三元运算只适用于简单的if else判断,再多一层 ...

  10. ent 基本使用十八 查询谓词

    ent 生成的代码包含了比较完整的查询谓词 字段谓词 Bool: =, != Numeric: =, !=, >, <, >=, <=, IN, NOT IN Time: =, ...