其他分享
首页 > 其他分享> > 九、查看容器内对象

九、查看容器内对象

作者:互联网

 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

标签:容器,ioc,查看,对象,spring,bean,Computer,context,com
来源: https://www.cnblogs.com/nanfeng66/p/16182529.html