首页 > 其他分享 >利用SpringBeanUtil 来获取 IOC 容器中的bean

利用SpringBeanUtil 来获取 IOC 容器中的bean

时间:2024-06-10 17:32:56浏览次数:13  
标签:容器 applicationContext getBean bean ApplicationContext SpringBeanUtil import IOC

有时候在代码中,不希望使用自动注入,而是手动获取Spring容器以及Spring容器中的某个对象

1、首先写一个class实现ApplicationContextAware#

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 @Component
public class SpringBeanUtil implements ApplicationContextAware {
 
    private static ApplicationContext applicationContext;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringBeanUtil.applicationContext = applicationContext;
    }
 
    /**
     * 通过名称在spring容器中获取对象
     * 
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }
 
}

2、在代码调用SpringBeanUtil的getBean获取指定的bean对象了

https://www.cnblogs.com/houchen/p/14005211.html

标签:容器,applicationContext,getBean,bean,ApplicationContext,SpringBeanUtil,import,IOC
From: https://www.cnblogs.com/isme-zjh/p/18240838

相关文章

  • 4_Spring Bean的初始化和销毁
    SpringBean的初始化和销毁1.Bean的初始化执行流程Spring提供了多种初始化和销毁的方法编写相关Bean代码:publicclassBean1implementsInitializingBean{@PostConstructpublicvoidinit1(){System.out.println("初始化1");}@Override......
  • 2_Bean的生命周期和常见的后处理器
    1.SpringBean生命周期各个阶段首先编写一个Bean:@ComponentpublicclassLifeCycleBean{publicLifeCycleBean(){System.out.println("Bean构造");}@Autowiredpublicvoidautowire(@Value("${JAVA_HOME}")StringjavaHome){......
  • Spring常用注解,自动扫描装配Bean
    1引入context命名空间(在Spring的配置文件中),配置文件如下:Xml代码xmlns:context="http://www.springframework.org/schema/context"http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd......
  • ApplicationContextAware获取IOC容器
    目录常见Aware实现ApplicationContextAware作用1、ApplicationContext是什么?2、ApplicationContextAware作用ApplicationContextAware使用常见Aware实现接口作用ApplicationContextAware获取当前应用的上下文对象EnvironmentAware获取环境变量,包括我们配置的以及......
  • Spring基础 - Spring核心之控制反转(IOC)
    Spring基础-Spring核心之控制反转(IOC)IoC(Inversionofcontrol)IoC不是Spring提出来的,它们在Spring之前其实已经存在了,只不过当时更加偏向于理论。Spring在技术层次将这个思想进行了很好的实现。 什么是IoC?IoC(InversionofControl)即控制反转/反转控制。它是一......
  • 1_关于BeanFactory与ApplicationContext的联系和区别
    BeanFactory与ApplicationContext1.容器和接口1.1BeanFactory与ApplicationContext的联系与区别:ConfigurableApplicationContext是ApplicationContext的子接口,而ApplicationContext接口又是BeanFactory的子接口。因此ConfigurableApplicationContext接口简介继承了Bean......
  • Spring家族框架——Spring3——IOC基于XML创建对象的方式
    ......
  • Java开发必读,谈谈对Spring IOC与AOP的理解
    本文分享自华为云社区《超详细的Java后台开发面试题之SpringIOC与AOP》,作者:GaussDB数据库。一、前言IOC和AOP是Spring中的两个核心的概念,下面谈谈对这两个概念的理解。二、IOC(InverseofControl)控制反转,也可以称为依赖倒置。所谓依赖,从程序的角度看,就是比如A要调用B的方法......
  • Java动态获取实现类 Class.forName(clazz).newInstance()和applicationContext.getBea
    Java动态获取实现类Class.forName(clazz).newInstance()和applicationContext.getBean,beanMap寻找方式,JavaMap定义和初始化方法1.定义枚举类MyServiceTypeEnum.javapackagecom.example.core.mydemo.bean;publicenumMyServiceTypeEnum{SUBMIT_ORDER_SUCCESS("s......
  • rabbitMq实现系统内的短信发送设计&动态获取BEAN
    rabbitMq实现系统内的短信发送设计&动态获取BEAN1.短信非系统的重要节点操作,可以在任务完成之后,比如下单成功,发送下单成功的mq消息,短信服务接收到mq消息,动态的判断该短信的code,通过全局公共的父类(调用中台等接口获取全部所有需要的对象参数),获取短信中的{mobile}等参数来替换短......