ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

九、查看容器内对象

2022-04-23 15:33:14  阅读:176  来源: 互联网

标签:容器 ioc 查看 对象 spring bean Computer context com


 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有