• 某些情况下我们要获取 IOC 容器中指定注解、类型、名字的 Bean
  • 要获取 IOC 容器中指定条件的 Bean 可以通过 ApplicationContext 相应的方法
@Autowired
private ApplicationContext applicationContext;

获取指定注解所有的 Bean

Map<String,Object> objectMap = applicationContext.getBeansWithAnnotation(Service.class);

获取指定注解所有的 Bean 的名字

String[] beanNames = applicationContext.getBeanNamesForAnnotation(Service.class);

获取容器中指定某类型、或实现某接口、或继承某父类所有的 Bean

Map<String, Test> objectMap = applicationContext.getBeansOfType(Test.class);

获取容器中指定某类型、或实现某接口、或继承某父类所有的 Bean 的名称

String[] beanNames = applicationContext.getBeanNamesForType(Service.class);

获取指定名字的 Bean

Test testbean = (Test)applicationContext.getBean("testbean");

获取指定名字、类型的 Bean,指定的类型可以是其父类或所实现的接口

Object object = applicationContext.getBean("testbean", TestInterface.class);

获取指定类型、或接口、或某类的子类的 Bean

Object object = applicationContext.getBean(TestInterface.class);

获取容器中 Bean 的数量

int beanCount = applicationContext.getBeanDefinitionCount();

获取容器中所有 Bean 的名字

String[] beanNames = applicationContext.getBeanDefinitionNames();

Spring:获取容器中的Bean的更多相关文章

  1. spring 在容器中一个bean依赖另一个bean 需要通过ref方式注入进去 通过构造器 或property

    spring  在容器中一个bean依赖另一个bean 需要通过ref方式注入进去 通过构造器 或property

  2. spring中获取容器中的Bean为什么前转成接口而不是实现类

    简单介绍一下上下文,userService是服务层接口有一个save方法,userServiceImpl是该接口的实现类重写了save方法. applicationContext.xml如图: 后台代 ...

  3. 在Bean中获取spring 容器 并通过容器获取容器中其他bean

    spring 提供了Awear 接口去 让bean 能感受到外界的环境.Awear 接口有很多实现,常用的有 ApplicationContextAware (可以通过实现这个接口去获取Applica ...

  4. Spring IOC容器中注入bean

    一.基于schema格式的注入 1.基本的注入方式 (属性注入方式) 根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求 <bean id ...

  5. Spring学习--实现 FactoryBean 接口在 Spring IOC 容器中配置 Bean

    Spring 中有两种类型的 bean , 一种是普通的 bean , 另一种是工厂 bean , 即 FactroyBean. 工厂 bean 跟普通 bean 不同 , 其返回的对象不是指定类的一 ...

  6. spring 框架通过new Object()获取applicationContext 中的bean方案

    工作中,需要手动创建一个对象,但用到了spring容器中对象的业务逻辑,这时候就要去通过获取容器中的对象.这时候,可以通过实例化一个实现了ApplicationContextAware接口的类获取sp ...

  7. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  8. SpringBoot 之 普通类获取Spring容器中的bean

    [十]SpringBoot 之 普通类获取Spring容器中的bean   我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...

  9. 获取Spring容器中的Bean协助调试

    在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试. 第一步:编写自定义监听器 /** * 监听serv ...

随机推荐

  1. gnuradio 创建动态库 libftd3xx.so

    首先还是创建好模块gr-kcd cd gr-kcd 打开CMakeLists.txt cmake_minimum_required(VERSION 2.6) project(gr-kcd CXX C) ...

  2. 【kafka】confluent_kafka重置offset

    之前写过两篇关于重置offset的博文,后来使用过程中都有问题. 经过各种尝试,终于找到了解决方案. 直接上代码: # coding=utf8 from confluent_kafka import ...

  3. java 得到目录路径的方法

    得到web项目的根目录路径 System.getProperty("user.dir")// String path = this.getServletContext().getR ...

  4. Wireless Penetration Testing(命令总结)

    1.对本书出现的无线网络涉及的命令做一总结 查看无线网卡( Create a monitor mode interface using your card as shown in the follow ...

  5. bzoj 1856

    做这题之前先看道高考真题(好像是真题,我记不清了) 例:已知一个由n个0和n个1排列而成的数列,要求对于任意k∈N*且k∈[1,2n],在前k个数中1的个数不少于0的个数,求当n=4时这样的数列的数量 ...

  6. linux安装python3+selenium

    安装笔记 当前安装使用centos7 安装python3 1.下载 [admin@ ~] wget https://www.python.org/ftp/python/3.6.0/Python-3.6 ...

  7. OrCAD Capture CIS 为库里的元器件添加新属性

    1.进入元器件编辑界面 2.菜单:Options > Part Properties... 3.在窗口User Properties中,点击按钮New... 4.在弹出的子窗口NewProper ...

  8. MAC 调用GCC 提示xcrun: error: invalid active developer path

    xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: ...

  9. 在 Python 中使用 JSON

    在 Python 中使用 JSON 本教程将会教我们如何使用 Python 编程语言编码和解码 JSON.让我们先来准备环境以便针对 JSON 进行 Python 编程. 环境 在我们使用 Pytho ...

  10. Python生成PASCAL VOC格式的xml标注文件

    Python生成PASCAL VOC格式的xml标注文件 PASCAL VOC数据集的标注文件是xml格式的.对于py-faster-rcnn,通常以下示例的字段是合适的: <annotatio ...