Spring_(4)属性配置细节之二

集合属性

  • 在Spring中可以通过一组内置的xml标签(例如:,或)来配置集合属性。
  • 配置java.util.List类型的属性,需要指定标签,在标签里包含一些元素,这些标签可以通过指定简单的常量值,通过指定对其他Bean的引用,通过指定内置Bean定义。通过指定空元素,甚至可以内嵌其他集合。
  • 数组的定义和List一样,都使用。
  • 配置java.util.Set需要使用标签,定义元素的方法与List一样。
  • java.util.Map通过标签定义,标签里可以使用多个作为子标签,每个条目包含一个键和一个值。
  • 必须在标签里定义键。
  • 因为键和值的类型没有限制,所以可以自由地为它们指定,,或元素。
  • 可以将Map的键和值作为的属性定义:简单常量使用key和value来定义;Bean引用通过key-ref和value-ref属性定义。
  • 使用定义java.util.Properties,该标签使用多个作为子标签,每个标签必须定义key属性。

使用utility scheme定义集合

  • 使用基本的集合标签定义集合时,不能将集合作为独立的Bean定义,导致其他Bean无法引用该集合,所以无法在不同Bean之间共享集合
  • 可以使用util schema里的集合标签定义独立的结合Bean,需要注意的是,必须在根元素里添加util schema定义

使用p命名空间

  • 为了简化xml文件的配置,也来越多的xml文件采用属性而非子元素配置信息。
  • Spring从2.5版本开始引入了一个新的p命名空间,可以通过元素属性的方式配置Bean的属性。
  • 使用p命名空间后,基于XML的配置方式将进一步简化

例子演示:

程序的整体结构

在这里插入图片描述

主要的java类和xml配置文件

Car.java

package com.spring.beans;

public class Car {

    private String brand;
    private String corp;
    private double price;
    private int maxSpead;

    public Car(String brand,String corp,double price){
        super();
        this.brand = brand;
        this.corp = corp;
        this.price = price;
    }


    public Car(String brand,String corp,int maxSpead){
        super();
        this.brand = brand;
        this.corp = corp;
        this.maxSpead = maxSpead;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public void setMaxSpead(int maxSpead) {
        this.maxSpead = maxSpead;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", corp='" + corp + '\'' +
                ", price=" + price +
                ", maxSpead=" + maxSpead +
                '}';
    }
}

Person.java

package com.spring.beans.collections;

import com.spring.beans.Car;

import java.util.List;

public class Person {

    private  String name;
    private int age;
    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 List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> car) {
        this.cars = car;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", cars=" + cars +
                '}';
    }


}

NewPerson.java

package com.spring.beans.collections;

import com.spring.beans.Car;

import java.util.Map;

public class NewPerson {

    private String name;
    private int age;

    private Map<String,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 Map<String, Car> getCars() {
        return cars;
    }

    public void setCars(Map<String, Car> cars) {
        this.cars = cars;
    }

    @Override
    public String toString() {
        return "NewPerson{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", cars=" + cars +
                '}';
    }
}

DataSource.java

package com.spring.beans.collections;

import java.util.Properties;

public class DataSource {

    private Properties properties;

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "DataSource{" +
                "properties=" + properties +
                '}';
    }
}

applicationContext.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"
       xmlns:p="http://www.springframework.org/schema/p"
       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.xsd">

    <!--配置bean
        class:bean 的全类名, 通过反射的方式 在IOC容器中创建Bean, 所以要求Bean中必须有无参数的构造器
        id: 标识容器中的 bean, id 唯一,
    -->
    <bean id ="helloworld" class="com.spring.beans.HelloWorld">
        <property name="name" value="Spring"></property><!--这个是通过setter方法进行最常用的属性注入-->
    </bean>

    <!--通过构造方法来配置bean的属性-->
    <bean id="car" class="com.spring.beans.Car">
        <constructor-arg value="Audi" index="0"></constructor-arg>
        <constructor-arg value="ShangHai" index="1"></constructor-arg>
        <constructor-arg value="300000" index="2"></constructor-arg>
    </bean>

    <!--使用构造器注入属性值可以指定参数的位置和类型!以区分重载的构造器!-->
    <bean id="car2" class="com.spring.beans.Car">
        <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
        <!--如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来-->
        <!--属性值也可以直接用<value>标签配置-->
        <constructor-arg type="java.lang.String">
            <value><![CDATA[<ShangHai^>]]></value>
        </constructor-arg>
        <constructor-arg type="int">
            <value>250</value>
        </constructor-arg>
    </bean>

    <bean id="person" class="com.spring.beans.Person">
        <property name="name" value="Tom"></property>
        <property name="age" value="24"></property>
        <!--可以使用 property 的 ref 属性建立 bean 之间的引用关系. -->

        <!--<property name="car" ref="car2"></property>-->

        <!--<property name="car">
            <ref bean="car2"/>
        </property>-->

        <!--内部bean,不能被外部引用,只能在内部使用-->
        <property name="car">
            <bean class="com.spring.beans.Car">
                <constructor-arg value="Ford"></constructor-arg>
                <constructor-arg value="Changan"></constructor-arg>
                <constructor-arg value="200000" type="double"></constructor-arg>
            </bean>
        </property>
        <property name="car.maxSpead" value="260"></property>

    </bean>

    <bean id="person2" class="com.spring.beans.Person">
        <constructor-arg value="Jerry"></constructor-arg>
        <constructor-arg value="25"></constructor-arg>

        <!--<constructor-arg ref="car"></constructor-arg>-->
        <!--测试赋值null-->
        <!--<constructor-arg><null/></constructor-arg>-->

        <constructor-arg ref="car"></constructor-arg>
        <!--为级联属性赋值,注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常,和Struts不同-->
        <property name="car.maxSpead" value="250"></property>


    </bean>


    <!--测试如何配置集合属性-->

    <bean id="person3" class="com.spring.beans.collections.Person">
        <property name="name" value="Mike"></property>
        <property name="age" value="27"></property>
        <property name="cars">
            <!--使用list节点为List类型的属性赋值-->
            <list>
                <ref bean="car"/>
                <ref bean="car2"/>
                <bean class="com.spring.beans.Car">
                    <constructor-arg value="Ford"></constructor-arg>
                    <constructor-arg value="Changan"></constructor-arg>
                    <constructor-arg value="200000" type="double"></constructor-arg>
            </bean>
            </list>
        </property>
    </bean>

    <!--配置Map 属性值-->
    <bean id="newPerson" class="com.spring.beans.collections.NewPerson">
        <property name="name" value="Rose"></property>
        <property name="age" value="28"></property>
        <property name="cars">
            <!--使用map 节点及map的entry 子节点配置 Map类型的成员变量-->
            <map>
                <entry key="AA" value-ref="car"></entry>
                <entry key="BB" value-ref="car2"></entry>
            </map>
        </property>
    </bean>
    
    <!--配置Properties属性值-->
    <bean id = "dataSource" class="com.spring.beans.collections.DataSource">
        <property name="properties">
            <!--使用props和prop 子节点来为Properties 属性赋值-->
            <props>
                <prop key="user">root</prop>
                <prop key="password">1234</prop>
                <prop key="jdbcUrl">jdbc:mysql:///test</prop>
                <prop key="driverClass">com.mysql.jdbc.Driver</prop>
            </props>
        </property>
    </bean>

    <!--配置单例的集合bean,以供多个bean进行引用,需要导入util命名空间-->
    <util:list id="cars">
        <ref bean="car"/>
        <ref bean="car2"/>
    </util:list>

    <bean id="person4" class="com.spring.beans.collections.Person">
        <property name="name" value="Jack"></property>
        <property name="age" value="29"></property>
        <property name="cars" ref="cars"></property>
    </bean>

    <!--通过p命名空间为bean的属性赋值,需要先导入p命名空间,相对于传统的配置方式更加的简洁-->
    <bean id="person5" class="com.spring.beans.collections.Person" p:age="30"
          p:name="Queen" p:cars-ref="cars"></bean>

    <!--<bean id ="helloworld2" class="com.spring.beans.HelloWorld">
        <property name="name" value="Spring"></property>
    </bean>-->
</beans>

Main.java

package com.spring.beans;

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

public class Main {
    public static void main(String[] args){

       /* //创建HelloWorld的一个对象
        HelloWorld helloWorld = new HelloWorld();
        //为name 属性赋值
        helloWorld.setName("atguigu");*/


        //1.创建Spring 的IOC容器对象
        //ApplicationContext 代表IOC容器,实际上是一个借口
        //ClassPathXmlApplicationContext: 是ApplicationContext 接口的实现类,该实现类从类路劲下来加载配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.从IOC容器中获取Bean 实例
        //利用id定位到IOC容器中的bean
        HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloworld");
        //利用类型返回IOC容器中的Bean,但要求IOC容器中必须只能由一个该类型的Bean
        //HelloWorld helloWorld = ctx.getBean(HelloWorld.class);//要求这个类在配置文件中是唯一的
        System.out.println(helloWorld);

        Car car = (Car)ctx.getBean("car");
        System.out.println(car);

        car = (Car) ctx.getBean("car2");
        System.out.println(car);

        Person person = (Person) ctx.getBean("person");
        System.out.println(person);

        Person person2 = (Person) ctx.getBean("person2");
        System.out.println(person2);

        //3.调用hello方法
        //helloWorld.hello();
    }
}

结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42036647/article/details/84073834
今日推荐