SpringBoot 上下文获取注入的Bean
- 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的更多相关文章
- java 从spring容器中获取注入的bean对象
java 从spring容器中获取注入的bean对象 CreateTime--2018年6月1日10点22分 Author:Marydon 1.使用场景 控制层调用业务层时,控制层需要拿到业务层在 ...
- SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式
前言: spring容器以xml的形式注入bean,然后可以在类中获取,获取的形式主要有二种:第一种最简单--采用@Resource 或@Autowired关键字在加载spring文件时将bean注入 ...
- springboot中如果使用了@Autowired注入了bean,则这个类也要为spring bean,new出来注入的bean为null
https://blog.csdn.net/Mr_Runner/article/details/83684088 问题:new出来的实例中含有@Autowired注入时,注入的Bean为null: 解 ...
- SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean
分类: [java]2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路 ...
- SpringBoot中获取上下文
在实际开发中,有时候会根据某个bean的名称或class到Spring容器中获取对应的Bean.这里只做个简单的记录,方便后续自查. @Component public class SpringCon ...
- SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)
先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...
- springboot的依赖注入报null的问题
最近使用springboot开发项目,使用到了依赖注入,频繁的碰到注入的对象报空指针,错误如下 java.lang.NullPointerException: null at com.mayihc.a ...
- web项目中获取spring的bean对象
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...
- 【SpringBoot源码分析】-Bean的加载过程
-- 以下内容均基于2.1.8.RELEASE版本 在<SpringBoot启动过程的分析>系列文章中简要的对SpringBoot整体的启动流程作了梳理,但并未针对诸多细节进行分析.前面的 ...
随机推荐
- javascript学习3、数据类型、数据类型转换、运算符
数据类型包括:基本数据类型和引用数据类型 基本数据类型指的是简单的数据段,引用数据类型指的是有多个值构成的对象. 当我们把变量赋值给一个变量时,解析器首先要确认的就是这个值是基本类型值还是引用类型值 ...
- 项目Beta冲刺(团队6/7)
项目Beta冲刺(团队) --6/7 作业要求: 项目Beta冲刺(团队) 1.团队信息 团队名 :男上加男 成员信息 : 队员学号 队员姓名 个人博客地址 备注 221600427 Alicesft ...
- Beta冲刺(3/7)——2019.5.24
所属课程 软件工程1916|W(福州大学) 作业要求 Beta冲刺(3/7)--2019.5.24 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万里 ...
- Easyui combobox 源码修改模糊查询v=1.34
原来的匹配方式: $.fn.combobox.defaults=$.extend({},$.fn.combo.defaults,{valueField:"value",textFi ...
- NameNode和SecondaryNameNode
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_37838429/artic ...
- 证明StringBuffer线程安全,StringBuilder线程不安全
证明StringBuffer线程安全,StringBuilder线程不安全证明StringBuffer线程安全StringBuilder线程不安全测试思想测试代码结果源码分析测试思想分别用1000个线 ...
- GAME-BASED LEARNING
https://collegestar.org/modules/game-based-learning Introduction Appalachian State University Game ...
- 每隔15s异步刷新手机页面
前台异步js发请求代码: <script type="text/javascript"> var lightArr; var lightHistoryArr; /* 每 ...
- 【C/C++】static关键字
首先static的最主要功能是隐藏,其次因为static变量存放在静态存储区,所以它具备持久性和默认值0. static性质 隐藏 当同时编译多个文件时,未加static前缀的全局变量和函数都具有全局 ...
- Numpy | 10 广播(Broadcast)
广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 下面的图片展示了数组 b 如何通过广播来与数组 a 兼容. 4x ...