1. import org.springframework.beans.BeansException;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.ApplicationContextAware;
  4. import org.springframework.stereotype.Component;
  5.  
  6. /**
  7. * 获取Spring上下文
  8. *
  9. * @author kelin.ll
  10. * @date on 2019/7/18
  11. */
  12. @Component
  13. public class ApplicationContextProvider implements ApplicationContextAware {
  14. /**
  15. * 上下文对象实例
  16. */
  17. private static ApplicationContext applicationContext;
  18.  
  19. @Override
  20. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  21. this.applicationContext = applicationContext;
  22. }
  23.  
  24. /**
  25. * 获取applicationContext
  26. *
  27. * @return
  28. */
  29. public static ApplicationContext getApplicationContext() {
  30. return applicationContext;
  31. }
  32.  
  33. /**
  34. * 通过name获取 Bean.
  35. *
  36. * @param name
  37. * @return
  38. */
  39. public static Object getBean(String name) {
  40. return getApplicationContext().getBean(name);
  41. }
  42.  
  43. /**
  44. * 通过class获取Bean.
  45. *
  46. * @param clazz
  47. * @param <T>
  48. * @return
  49. */
  50. public static <T> T getBean(Class<T> clazz) {
  51. return getApplicationContext().getBean(clazz);
  52. }
  53.  
  54. /**
  55. * 通过name,以及Clazz返回指定的Bean
  56. *
  57. * @param name
  58. * @param clazz
  59. * @param <T>
  60. * @return
  61. */
  62. public static <T> T getBean(String name, Class<T> clazz) {
  63. return getApplicationContext().getBean(name, clazz);
  64. }
  65. }

测试用例:

  1. import com.provider.ServiceProviderApplication;import lombok.extern.slf4j.Slf4j;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  6.  
  7. /**
  8. * @author kelin.ll
  9. * @date on 2019/7/18
  10. */
  11. @RunWith(SpringJUnit4ClassRunner.class)
  12. @SpringBootTest(classes = ServiceProviderApplication.class)
  13. @Slf4j
  14. public class ApplicationContextProviderTest {
  15. @Test
  16. public void getBeanByNameTest(){
  17. TaskService taskService = (TaskService)ApplicationContextProvider.getBean("taskService");
  18. System.out.println(taskService.getAll());
  19. }
  20.  
  21. @Test
  22. public void getBeanByClassTest(){
  23. TaskService taskService = ApplicationContextProvider.getBean(TaskService.class);
  24. System.out.println(taskService.getAll());
  25. }
  26.  
  27. @Test
  28. public void getBeanByNameAndClassTest(){
  29. TaskService taskService = ApplicationContextProvider.getBean("taskService",TaskService.class);
  30. System.out.println(taskService.getAll());
  31. }
  32. }

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. javascript学习3、数据类型、数据类型转换、运算符

    数据类型包括:基本数据类型和引用数据类型 基本数据类型指的是简单的数据段,引用数据类型指的是有多个值构成的对象. 当我们把变量赋值给一个变量时,解析器首先要确认的就是这个值是基本类型值还是引用类型值 ...

  2. 项目Beta冲刺(团队6/7)

    项目Beta冲刺(团队) --6/7 作业要求: 项目Beta冲刺(团队) 1.团队信息 团队名 :男上加男 成员信息 : 队员学号 队员姓名 个人博客地址 备注 221600427 Alicesft ...

  3. Beta冲刺(3/7)——2019.5.24

    所属课程 软件工程1916|W(福州大学) 作业要求 Beta冲刺(3/7)--2019.5.24 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万里 ...

  4. Easyui combobox 源码修改模糊查询v=1.34

    原来的匹配方式: $.fn.combobox.defaults=$.extend({},$.fn.combo.defaults,{valueField:"value",textFi ...

  5. NameNode和SecondaryNameNode

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_37838429/artic ...

  6. 证明StringBuffer线程安全,StringBuilder线程不安全

    证明StringBuffer线程安全,StringBuilder线程不安全证明StringBuffer线程安全StringBuilder线程不安全测试思想测试代码结果源码分析测试思想分别用1000个线 ...

  7. GAME-BASED LEARNING

    https://collegestar.org/modules/game-based-learning Introduction   Appalachian State University Game ...

  8. 每隔15s异步刷新手机页面

    前台异步js发请求代码: <script type="text/javascript"> var lightArr; var lightHistoryArr; /* 每 ...

  9. 【C/C++】static关键字

    首先static的最主要功能是隐藏,其次因为static变量存放在静态存储区,所以它具备持久性和默认值0. static性质 隐藏 当同时编译多个文件时,未加static前缀的全局变量和函数都具有全局 ...

  10. Numpy | 10 广播(Broadcast)

    广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 下面的图片展示了数组 b 如何通过广播来与数组 a 兼容. 4x ...