Spring学习--使用 utility scheme 定义集合及 p命名空间

util schema 定义集合:

  1. 使用基本的集合标签定义集合时 , 不能将集合作为独立的 Bean 定义 , 导致其他 Bean 无法引用该集合 , 所以无法在不同 Bean 之间共享集合。
  2. 可以用util schema 里的集合标签定义独立的集合 Bean。 需要注意的是 , 必须在 <beans> 根元素里添加 util schema 定义(xmlns:util="http://www.springframework.org/schema/util)。引入util命名空间。代码如下:

spring.xml文件的代码如下:

<?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.0.xsd">

    <bean id="car" class="Car">
        <constructor-arg value="baoma" ></constructor-arg>
        <constructor-arg value="12" ></constructor-arg>
        <constructor-arg value="red" ></constructor-arg>
    </bean>
    <!--用构造器去配置属性-->
    <bean id="car1" class="Car">
        <constructor-arg value="bentian"></constructor-arg>
        <constructor-arg value="13"></constructor-arg>
        <constructor-arg value="blue"></constructor-arg>
    </bean>
    <!--配置单例的集合bean,以供多个bean进行引用,需要导入util命名空间-->
    <util:list id="cars">
        <ref bean="car"/>
        <ref bean="car1"/>
    </util:list>
    <bean id="person" class="collection.Person">
        <property name="name" value="chen"></property>
        <property name="age" value="56"></property>
        <property name="cars" ref="cars"/>
    </bean>
  
</beans>

 

person类的文件:

package collection;

import java.util.List;
import java.util.Map;

public class Person {
    public String name;
    public int age;

    public List getCars() {
        return cars;
    }

    public void setCars(List cars) {
        this.cars = cars;
    }

    public List 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;
    }

    @Override
    public Strin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值