说明:
(1)如何查看容器内有多少个对象?
前面介绍了如何在容器内创建对象和设置对象之间的关系;但是,所有这些信息,都是我们自动脑补去想象的;如果一个项目足够大,对象很多,单靠去想就很吃力,那么我们如何知道当前容器中到底有哪些对象,这些对象又是什么类型的呐?
(2)主要内容包括:【context.getBeanDefinitionNames()】:获取容器中所有id的数组;【context.getBean(beanName).getClass().getName()】获取bean的类型;【context.getBean(beanName).toString()】:获取bean的具体内容;
本篇博客的代码沿用【Spring IoC容器与Bean管理14:注入集合对象;】中的s05;
目录
1.为了演示:在applicationContext.xml中编写如下内容:
(1)【context.getBeanDefinitionNames()】:获取容器中所有id的数组;
(3)【context.getBean(beanName).getClass().getName()】获取bean的类型
(4)【context.getBean(beanName).toString()】:获取bean的具体内容
1.为了演示:在applicationContext.xml中编写如下内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="c1" class="com.imooc.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="联想"/>
<constructor-arg name="type" value="台式机"/>
<constructor-arg name="sn" value="8389283012"/>
<constructor-arg name="price" value="3085"/>
</bean>
<bean class="com.imooc.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="戴尔"/>
<constructor-arg name="type" value="台式机"/>
<constructor-arg name="sn" value="8389283012"/>
<constructor-arg name="price" value="3085"/>
</bean>
<bean class="com.imooc.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="宏碁"/>
<constructor-arg