千家信息网

SpringBoot怎么使用ApplicationContext获取bean对象

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,小编给大家分享一下SpringBoot怎么使用ApplicationContext获取bean对象,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!使用ApplicationContex
千家信息网最后更新 2026年11月02日SpringBoot怎么使用ApplicationContext获取bean对象

小编给大家分享一下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

@Servicepublic class DemoService {    public void save(){        System.out.println("DemoService save");    }}

CommonClass

@Componentpublic class CommonClass {    @Resource    private DemoService demoService;    public void fun(){        System.out.println("fun");        demoService.save();    }}

Controller

@Resourceprivate CommonClass commonClass;@ResponseBody@GetMapping("/fun")public void fun(){    commonClass.fun();}

下面代码不能正常运行

DemoService

@Servicepublic 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 对象的代码就错误了。

解决方案

新建工具类

@Componentpublic 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

@Servicepublic 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中下面代码可以正确运行:

看完了这篇文章,相信你对"SpringBoot怎么使用ApplicationContext获取bean对象"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

对象 代码 方式 运行 工具 工厂 方案 篇文章 而是 解决方案 生成 这样一来 一来 两个 再次 名字 完了 实际 手工 方法 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广州万易互联网科技有限公司 中关村互联网 科技集团 新华互联网科技学校 易互联网科技有限公司 兼蜜互联网科技 深圳期待互联网科技 互联网科技股有哪些 蓝色互联网科技 安控科技rtu产品工业互联网 互联网科技寓意 承德科技产业互联网发展 银川新华互联网科技学校联系电话 畅思互联网科技 浙南科技城互联网大厦建设工程 山东子淼互联网科技有限公司 山东星卡互联网科技有限公司官网 智帮互联网科技公司最近动向 普陀区互联网教育科技管理办法 盐城市攀科互联网科技有限公司 科技互联网专业务虚会材料 重生之互联网科技帝国 朗新科技与工业互联网 互联网科技金融公司 米客互联网科技 互联网科技logo 互联网科技博客 卓翼科技产业互联网合作 无锡工业互联网科技有限公司 深圳互联网科技园地址 长沙易易互联网科技有限公司
0