微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

九、查看容器内对象

 String[] beanNames = context.getBeanDeFinitionNames(); 获取容器内所有beanId数组,返回一个string数组

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="c1" class="com.spring.ioc.entity.Computer">
        <constructor-arg name="brand" value="联想"></constructor-arg>
        <constructor-arg name="price" value="3085"></constructor-arg>
        <constructor-arg name="sn" value="8389283012"></constructor-arg>
        <constructor-arg name="type" value="台式机"></constructor-arg>
    </bean>

    <bean class="com.spring.ioc.entity.Computer">
        <constructor-arg name="brand" value="微星"></constructor-arg>
        <constructor-arg name="price" value="2563"></constructor-arg>
        <constructor-arg name="sn" value="5689242302"></constructor-arg>
        <constructor-arg name="type" value="台式机"></constructor-arg>
    </bean>

    <bean class="com.spring.ioc.entity.Computer">
        <constructor-arg name="brand" value="华硕"></constructor-arg>
        <constructor-arg name="price" value="8063"></constructor-arg>
        <constructor-arg name="sn" value="4246402302"></constructor-arg>
        <constructor-arg name="type" value="笔记本电脑"></constructor-arg>
    </bean>

    <bean id="company" class="com.spring.ioc.entity.Company">
        <!--注入list或set集合,认使用ArrayList或LinkedHashSet类型-->
        <property name="rooms">
            <set>
                <value>2001-总裁办</value>
                <value>2003-总经理办公室</value>
                <value>2010-研发部会议室</value>
                <value>2001-总裁办</value>
            </set>
        </property>
        <property name="computers">
            <!--注入Map集合,认使用LinkedHashMap类型-->
            <map>
                <entry key="dev-88172" value-ref="c1"></entry>
                <!--内置bean设置ref,需确保这个bean不会被其他对象关联-->
                <entry key="dev-88173">
                    <bean class="com.spring.ioc.entity.Computer">
                        <constructor-arg name="brand" value="联想"></constructor-arg>
                        <constructor-arg name="price" value="5088"></constructor-arg>
                        <constructor-arg name="sn" value="4243433435"></constructor-arg>
                        <constructor-arg name="type" value="笔记本"></constructor-arg>
                    </bean>
                </entry>
            </map>
        </property>

        <property name="info">
            <props>
                <prop key="phone">1008866</prop>
                <prop key="address">长江路80号南阳理工学院</prop>
                <prop key="website">http://www.xxx.com</prop>
            </props>
        </property>
    </bean>
</beans>
package com.spring.ioc;

import com.spring.ioc.entity.Company;
import com.spring.ioc.entity.Computer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClasspathXmlApplicationContext;

public class SpringApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClasspathXmlApplicationContext("classpath:applicationContext.xml");
        String[] beanNames = context.getBeanDeFinitionNames();
        for(String beanName:beanNames){
            System.out.println(beanName);
        }
        Computer computer = context.getBean("com.spring.ioc.entity.Computer", Computer.class);
        System.out.println(computer.getBrand());
        Computer computer1 = context.getBean("com.spring.ioc.entity.Computer#1", Computer.class);
        System.out.println(computer1.getBrand());
    }
}

 getBeanDeFinitionNames()方法不会获取到内置bean的id,可以获取多个匿名bean的id按顺序排列

 使用context.getBean()获取匿名bean时,如果不加编号,则获取到第一个匿名bean,也可以添加编号#1获取具体需要的匿名bean

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐