加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:13:22 所属栏目:大数据 来源:网络整理
导读:?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.s
<?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() {
    }

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

     String getName() {
        return name;
    }

    void setName(String name) {
         name;
    }

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

Student.java

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

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

Main.java

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

 Main {
    static  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中都有什么:

?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读