普通对象使用spring容器中的对象的实现方法
作者:我想问问天 发布时间:2023-06-17 12:27:20
引语:
工作中有时候需要在普通的对象中去调用spring管理的对象,但是在普通的java对象直接使用@Autowired或者@Resource的时候会发现被注入的对象是null,会报空指针。我们可以简单的理解为spring是一个公司,它管理的对象就是它的员工,而普通的java对象是其他公司的员工,如果其他公司要找spring公司的员工一起共事没有经过spring公司的同意肯定是不行的。
解决方式:
方法一:如果这个普通对象可以被spring管理的话,最好是直接交给spring管理,这样spring管理的bean中注入其他的bean是没有问题的。
方法二:当我们的普通对象没有办法交给spring管理的时候,我们可以创建一个公共的springBeanUtil专门为普通对象提供spring的员工(有点像spring公司的外包部门,把对象外包给其他公司使用,哈哈)。
@Service
public class SpringBeanUtil implements ApplicationContextAware {
public static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}
// 这里使用的是根据class类型来获取bean 当然你可以根据名称或者其他之类的方法 主要是有applicationContext你想怎么弄都可以
public static Object getBeanByClass(Class clazz) {
return applicationContext.getBean(clazz);
}
}
这个util呢,其实就是实现了ApplicationContextAware接口,有小伙伴要问了这个接口是干嘛的?这里给出链接地址,ApplicationContextAware参考资料。然后我也将文档中的解释给摘录过来了
public interface ApplicationContextAware extends Aware
Interface to be implemented by any object that wishes to be notified of the ApplicationContext that it runs in.
Implementing this interface makes sense for example when an object requires access to a set of collaborating beans. Note that configuration via bean references is preferable to implementing this interface just for bean lookup purposes.
This interface can also be implemented if an object needs access to file resources, i.e. wants to call getResource, wants to publish an application event, or requires access to the MessageSource. However, it is preferable to implement the more specific ResourceLoaderAware, ApplicationEventPublisherAware or MessageSourceAware interface in such a specific scenario.
Note that file resource dependencies can also be exposed as bean properties of type Resource, populated via Strings with automatic type conversion by the bean factory. This removes the need for implementing any callback interface just for the purpose of accessing a specific file resource.
ApplicationObjectSupport is a convenience base class for application objects, implementing this interface.
大概意思就是说只要实现了ApplicationContextAware接口的类,期望被告知当前运行的applicationContext是什么。然后又说了如果是想要获取资源最好是用ResourceLoaderAware, ApplicationEventPublisherAware or MessageSourceAware 这几个接口,最后还来了一句我们知道你们要使用这些接口,所以我们帮你弄了一个实现了这些接口的抽象类ApplicationObjectSupport(在spring-context的jar包中)。这里说得很清楚要使用bean的话,实现ApplicationContextAware,因为我们这里不需要使用静态资源之类的所以我们就不用spring为我们提供的ApplicationObjectSupport了,有兴趣的可以自己研究下。
我们这里简单的看一下ApplicationContextAware类里面都有啥?
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
发现就一个方法,spring初始化的时候会将当前的applicationContext传给ApplicationContextAware的setApplicationContext方法,所以只要实现类将这个applicationContext拿到了,就可以通过class类型或者class的名称来获取到spring中的bean了。原理其实很简单吧。使用的时候我们可以调用spring中的bean。如下:
Test test = (Test) SpringBeanUtil.getBeanByClass(Test.class);
来源:https://segmentfault.com/a/1190000019366213
猜你喜欢
- 目录Navigator 的 push 和 pop方法代码实现最终效果Navigator 的 push 和 pop方法Navigator 导航
- 废话开篇:iOS与android在实现列表界面的时候是有重用机制的,目的就是减少内存开销,用时间换空间。个人感觉flutter并没有特别强调
- 一个是新浪微博,腾讯微博的分享按钮,一个是他们的绑定情况(其实就是是否授权)。点击微博分享中新浪或腾讯按钮,就进行相应的授权(若没授权),显
- 目标效果: 点击动画按钮之后每张牌各自旋转 散开到屏幕上半部分的任意位置之后回到初始位置 比较像LOL男刀的技能动画 : )1: 创建卡牌对
- webp格式图片webp格式图片是google推出的,相比jpg png有着巨大的优势,同样质量的图片webp格式的图片占用空间更小,在像电
- 本文实例为大家分享了flutter实现appbar下选项卡切换的具体代码,供大家参考,具体内容如下TabBar 、Tab、TabBarVie
- .c 源程序 ----- 编译 ----- 链接 ---- exe ----运行 -------->程序翻译环境和执行环境翻译环境:源
- 相比于直线检测,直线拟合的最大特点是将所有数据只拟合出一条直线void fitLine( InputArray points, Output
- [LeetCode] 2. Add Two Numbers 两个数字相加You are given two non-empty&n
- 简介对于一个APP来说,肯定会有一个AppBar,这个AppBar一般包含了APP的导航信息等。虽然我们可以用一个固定的组件来做为AppBa
- 协议做如下规定:规定数据协议:序列号 长度 状态字 数据长度 数据1 &n
- 这篇会深化View拖拽实例,利用Flutter Animation、插值器以及AnimatedBuilder教大家实现带动画的抽屉效果。先来
- 关于UIToolbarToolBar工具栏是视图View的属性,可以在工具栏上添加工具栏按钮Bar Button Item(可以是自定义的C
- 背景在接口请求过程中,传递json对象,springboot转换为实体VO对象后,所有属性都为null。post请求:后台接收请求:当时就懵
- 前言之前我们提到了 CustomPaint er 的 Paint 可以使用渐变(GradientShader)来填充绘制的图形,本篇我们来介
- 一、Flutter代码的启动起点我们在多数的业务场景下,使用的都是FlutterActivity、FlutterFragment。在在背后,
- 一、前言在Spring中,事务有两种实现方式:编程式事务管理: 编程式事务管理使用TransactionTemplate可实现更细
- 首先打开 Visual Studio Installer 可以看到vs2022 只支持安装4.6及以上的版本,如图所示。那么该如何安装4.6
- 已知两个链表list1和list,2,各自非降序排列,将它们合并成另外一个链表list3,并且依然有序,要求保留所有节点。实现过程中,lis
- 已知字符串“aabbbcddddeeffffghijklmnopqrst”编程找出出现最多的字符和次数,要求时间复杂度小于O(n^2)/**