获取Spring容器中的实例
1. 介绍
在Spring环境中想要获取一个类的实例,如果直接new出来,只是创建了一个新的对象并不是Spring容器中的实例,因此如果类中使用了@Value ,@Autowired等注解是不起作用的,所以需要获取一个注入在Spring容器中的实例。
2. 实现
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;/*** @describe: 获取springcontext中的bean*/
@Component
public class SpringContextUtil implements ApplicationContextAware {public static ApplicationContext context;@Overridepublic void setApplicationContext(ApplicationContext context) throws BeansException {SpringContextUtil.context = context;}/*** 获取容器中的实例* @param beanId 注入在Spring容器中的bean的ID 默认为类名首字母小写* @param clazz 获取的bean的实际的类的class */public static <T> T getBean(String beanId, Class<T> clazz){return context.getBean(beanId, clazz);}public static ApplicationContext getContext(){return context;}
}
获取Spring容器中的实例相关推荐
- 【SpringBoot】在普通类中获取spring容器中的bean
这段时间公司搞封闭开发,做一个联通总部的客服系统项目,是基于springboot的.在开发工程中遇到一个页面datagrid数据排序的功能,因为有多个表的数据都要用到排序功能,于是我就写了一个排序功能 ...
- SpringBoot 之 普通类获取Spring容器中的bean
SpringBoot 之 普通类获取Spring容器中的bean 转载于:https://www.cnblogs.com/lwmp/p/8892927.html
- 工具类:获取 spring 容器中 bean
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. package com.orange.utils;import org.springframewor ...
- 在普通类中获取spring容器中的bean
在普通类中获取spring容器中的bean 1.工具类 package com.itheima.hchat.util;import org.springframework.beans.BeansExc ...
- spring boot获取spring容器中的bean对象
我们知道如果我们要在一个类使用 spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接 ...
- Spring Boot中普通类获取Spring容器中的Bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...
- java静态方法中获取spring容器中的bean
获取上下文工具类: package com.hikvision.pvia.kafka.utils;import org.springframework.beans.BeansException; im ...
- 在servlet中或者在filter中获取spring容器中的bean
1 配置spring文件 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate ...
- 小孩的奇思妙想:将枚举注入到spring容器中
背景 笔者目前做的项目中有许多根据前台传参,然后根据参数获取不同实现类,取执行不同的代码逻辑,代码类似如下: 下面展示一些. 现有实现 /*** 执行逻辑** @param type : 实现类类型* ...
最新文章
- 使用purge_relay_logs 清理relay log
- MySQL显示ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)解决方法
- pve远程连接 spcie_proxmox折腾 篇一:解决j3455直通iommu分组问题,PVE内核编译教程...
- 【OpenCV3】如何给图像添加(不)透明度通道
- Android性能优化工具
- 产品经理不得不知的APP数据分析及报表设计基础
- Hejr 6-8 - aamsaafanqs Blog - Blogster
- Atitit截屏功能的设计解决方案
- 87-区间线段树(板子)--那个苑区的人最瘦
- 禁止ensp弹出提示
- 10年+,阿里沉淀出怎样的搜索引擎?
- qt使用样式表来设置不规则按钮
- python for循环例子_Python for循环生成列表的实例
- java 上传断点续传_JAVA大文件上传断点续传解决方案
- BULK INSERT, 实战手记:让百万级数据瞬间导入SQL Server
- 50道经典计算机网络面试题梳理
- Video Caption(跨模态视频摘要/字幕生成)
- HDU3338Kakuro Extension(最大流+边的流量)
- HTML CSS 布局
- PC、手机老是掉线该如何排查?
热门文章
- 双11购物节国外剁手党同狂欢 阿里云视频云电商直播实时字幕
- J. Jesting Jabberwocky
- 抽象类定义?为什么要使用抽象类?
- redhat8版防火墙管理工具( iptables/firewalld)
- Python3,网站搭建之数据库表设计及数据存储!文末的彩蛋,我酸了~
- Android 跳转基础应用(联系人,日历)
- 无法打开内核设备“\\.\VMCIDev\VMX”: 操作成功完成。是否在安装 VMware Workstation 后重新引导?模块“DevicePowerOn”启动失败。
- Android Studio连接手机模拟器
- css动画西游记行走
- HFSS一些使用技巧总结