SpringBoot如何使用ApplicationContext获取bean对象
作者:fwhui 发布时间:2023-06-28 20:36:55
标签:SpringBoot,ApplicationContext,bean
使用ApplicationContext获取bean对象
编写一个ApplicationContextFactory工厂类
public class ApplicationContextFactory{
private static ApplicationContext applicationContext = null;
public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
}
在SpringBoot的启动类中设置ApplicationContext
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext app = SpringApplication.run(Application.class, args);
ApplicationContextFactory.setApplicationContext(app);
}
}
通过ApplicationContextFactory获取SpringApplication从而获取bean对象
ApplicationContext applicationContext=ApplicationContextFactory.getApplicationContext();
Clazz clazz = applicationContext.getBean(Clazz.class);
SpringBoot Bean注入的深入研究
下面代码可正常运行
DemoService
@Service
public class DemoService {
public void save(){
System.out.println("DemoService save");
}
}
CommonClass
@Component
public class CommonClass {
@Resource
private DemoService demoService;
public void fun(){
System.out.println("fun");
demoService.save();
}
}
Controller
@Resource
private CommonClass commonClass;
@ResponseBody
@GetMapping("/fun")
public void fun(){
commonClass.fun();
}
下面代码不能正常运行
DemoService
@Service
public class DemoService {
public void save(){
System.out.println("DemoService save");
}
}
CommonClass
public class CommonClass {
@Resource
private DemoService demoService;
public void fun(){
System.out.println("fun");
demoService.save();
}
}
Controller
@ResponseBody
@GetMapping("/fun")
public void fun(){
CommonClass commonClass = new CommonClass();
commonClass.fun();
}
比较
比较两个代码发现后者与前者的区别:因后者的CommonClass 没有使用@Component标注,所以在Controller中不能才用注入方式生成CommonClass对象,而是才用new的方式生成了该对象。
这样一来,CommonClass 对象是手工创建,所以在它内部注入DemoService 对象的代码就错误了。
解决方案
新建工具类
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext act;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
act = applicationContext;
}
/**
* 根据bean的名字获取工厂中对应的bean对象
* @param beanName
* @return
*/
public static Object getBean(String beanName){
return act.getBean(beanName);
}
}
注:实际测试发现上面代码中的static不能省略
DemoService
@Service
public class DemoService {
public void save(){
System.out.println("DemoService save");
}
}
CommonClass
public class CommonClass {
@Resource
private DemoService demoService;
public void fun(){
DemoService demoService = (DemoService) ApplicationContextUtil.getBean("demoService");
System.out.println("fun");
demoService.save();
}
}
此处不再采用注入的方式获取DemoService对象,而是通过工具类的方式
Controller
@ResponseBody
@GetMapping("/fun")
public void fun(){
CommonClass commonClass = new CommonClass();
commonClass.fun();
}
再次运行程序,一切正常
应用
在SpringBoot整合Shiro的案例中,自定义Realm时,需要使用Service的对象。因为自定义的Realm类不能使用@Component之类的注解注释,所以使用本案例介绍的方法是正确的解决方案。尽管在1.6.0的shiro-all中下面代码可以正确运行:
来源:https://blog.csdn.net/qq_40804005/article/details/100164417


猜你喜欢
- 两种情况setState() 能在 build() 中直接调用吗?答案是能也不能。来看一段简单的代码:import 'package
- 前言前面小空带大家了解了TextView的属性,实践了一些自定义粗细,阴影效果,链接形文字。但这还仅仅局限在用她本身的能力来实现的,那么她还
- 本文介绍了Android ItemDecoration 实现分组索引列表的示例代码,分享给大家。具体如下:先来看看效果:我们要实现的效果主要
- 与任何程序设计语言一样,Java使用条件语句和循环结构确定控制流。本文将简单讲解条件、循环和switch。一、块作用域块(block),即复
- 本文实例讲述了C#多线程学习之操纵一个线程的方法。分享给大家供大家参考。具体实现方法如下:下面我们就动手来创建一个线程,使用Thread类创
- 添加MyBatis的代码并修改以下部分:1.添加MyBatisConfigpackage myshop.config;import java
- 1、判断实体对象是否为空2、判断对象所有属性是否为空3、特别注意,实体类中如果有基本数据类型,会影响判断package com.liuxd.
- engine的实现结构elasticsearch对于索引中的数据操作如读写get等接口都封装在engine中,同时engine还封装了索引的
- 思想利用栈和队列都可以实现树的迭代遍历。递归的写法将这个遍历的过程交给系统的堆栈去实现了,所以思想都是一样的、无非就是插入值的时机不一样。利
- 简介在文章《GraalVM和Spring Native尝鲜,一步步让Springboot启动飞起来,66ms完成启动》中,我们介绍了如何使用
- 本文实例讲述了Java高级特性之反射机制。分享给大家供大家参考,具体如下:老规矩我们还是先提出几个问题,一门技术必然要能解决一定的问题,才有
- 一、简介Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能。Spring Cache提供
- 本文实例讲述了C#实现基于XML配置MenuStrip菜单的方法。分享给大家供大家参考。具体如下:1.关于本程序的说明用XML配置MenuS
- 仿QQ侧滑删除效果图1.自定义listviewpublic class DragDelListView extends ListView {
- 本文实例为大家分享了java实现上传网络图片到微信临时素材的具体代码,供大家参考,具体内容如下package org.afuos.playc
- 一、前言文稿扫描大家用的都比较频繁、想是各种证件、文件都可以通过扫描文稿功能保存到手机。相比直接拍照,在扫描文稿时,程序会对图像进行一些矫正
- 最近因考虑接口安全问题,有实现给WEB API实现统一的参数鉴权功能,以防止请求参数被篡改或重复执行,参数鉴权方法基本与常见的鉴权思路相同,
- 1 引入 pom 包<dependency> <groupId>io.github.res
- 这篇文章主要介绍了配置springboot项目使用外部tomcat过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作
- 最近为公司做的一个Demo里面用到了ScrollView嵌套了GridView和ListView,然而在嵌套的时候我发现GridView和L