首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring之配置单例的集合bean,以供多个bean进行引用

spring之配置单例的集合bean,以供多个bean进行引用

作者头像
西西嘛呦
发布2020-08-26 09:57:56
3660
发布2020-08-26 09:57:56
举报
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
    
    <bean id="car1" class="com.gong.spring.beans.Car">
        <property name="name" value="baoma"></property>
    </bean>
    <bean id="car2" class="com.gong.spring.beans.Car">
        <property name="name" value="benchi"></property>
    </bean>
    <bean id="car3" class="com.gong.spring.beans.Car">
        <property name="name" value="binli"></property>
    </bean>
    
    <bean id="student" class="com.gong.spring.beans.Student">
        <property name="name" value="tom"></property>
        <property name="age" value="12"></property>
        <property name="score" value="98.00"></property>
        <property name="cars">
            <!-- 使用List节点为List集合属性赋值 
            <list>
                <ref bean="car1"/>
                <ref bean="car2"/>
                <ref bean="car3"/>
            </list>
        </property>
        
    </bean>

</beans>

这种情况下,是在一个bean的里面进行配置的,假设现在我们有另外一个bean,也需要使用List集合里的bean,那么应该怎么做呢?

首先,引入命名空间util,在applicationContext.xml下方的namespace里面,勾选下即可。如果没有,则需要安装Spring Tool Suite(STS) for Eclipse,具体装法可百度。

然后配置一个公用集合bean:

    <util:list id="cars">
        <ref bean="car1"/>
        <ref bean="car2"/>
        <ref bean="car3"/>
    </util:list>

此时可去掉上述代码中的红色部分,并这样使用:

<property name="cars" ref="cars">

补充其它代码:

Car.java

package com.gong.spring.beans;

public class Car {
    
    public Car() {
    }

    public Car(String name) {
        this.name = name;
    }
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + "]";
    }
    
}

Student.java

package com.gong.spring.beans;
import java.util.List;
import java.util.Map;

public class Student {
    
    private String name;
    private int age;
    private double score;
    private List<Car> cars;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public List<Car> getCars() {
        return cars;
    }
    public void setCars(List<Car> cars) {
        this.cars = cars;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", score=" + score + ", cars=" + cars + "]";
    }
    
    
}

Main.java

package com.gong.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        //1.创建spring的IOC容器对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.从容器中获取Bean实例
        Student student = (Student) ctx.getBean("student"); 
        System.out.println(student.toString());
    }
}

输出:

顺便看一下util中都有什么:

对于不同的集合,按照其相应的语法进行配置即可。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档