Spring如何配置bean属性

在Spring的bean配置文件中,每个bean必须有一个唯一标识的名称或者id,以及一个完全限定的类名,用来让Ioc容器对其进行实例化。

例如有一个简单的Bean:

package com.gisxx.beans;

public class Fruit {

    private String name;
    private double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }

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

}

使用property元素

通过property元素来给bean注入属性,bean必须要有相应的setter方法并且有不带参数的构造函数。

1、使用property元素的value子元素来指定属性值

    <!-- 使用property标签 -->
    <bean id="fruit1" class="com.gisxx.beans.Fruit">
        <property name="name">
            <value>苹果</value>
        </property>
        <property name="price">
            <value>5.5</value>
        </property>
    </bean>

2、使用简化的property元素,用value属性来指定属性值

    <!-- 使用简化的property标签 -->
    <bean id="fruit2" class="com.gisxx.beans.Fruit">
        <property name="name" value="橘子"></property>
        <property name="price" value="6.6"></property>
    </bean>

使用构造方法

使用constructor-arg 元素来指定bean属性必须要有带参数的构造方法。可以通过index属性来指定参数顺序,如果没有index属性则先后顺序必须和构造方法参数顺序一致。

向Fruit.java添加构造方法:

    public Fruit(String name, double price) {
        super();
        this.name = name;
        this.price = price;
    }

bean配置文件:

    <!-- 使用构造方法 -->
    <bean id="fruit4" class="com.gisxx.beans.Fruit">
        <constructor-arg index="0">
            <value>香蕉</value>
        </constructor-arg>
        <constructor-arg index="1">
            <value type="double">3.4</value>
        </constructor-arg>
    </bean>

使用P命名空间

使用p标签简化bean配置文件,通过p:(属性名)形式来设置bean属性必须在beans中添加p命名空间:xmlns:p=”http://www.springframework.org/schema/p”。

<!-- 使用p命名空间 -->
<bean id="fruit3" class="com.gisxx.beans.Fruit" p:name="桃子" p:price="8.8" />

引用其它bean

如果我们有一个Person类,其中fruit代表该person喜欢的水果,代码如下


public class Person {

    private String name;
    private int age;
    private Fruit fruit;


    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 Fruit getFruit() {
        return fruit;
    }
    public void setFruit(Fruit fruit) {
        this.fruit = fruit;
    }

}

在bean配置文件中配置Person类bean可以在配置fruit属性时使用ref属性引用创建好了的外部fruit类bean。

    <bean id="person1" class="com.gisxx.beans.Person">
        <property name="name" value="张三"></property>
        <property name="age" value="22"></property>
        <property name="fruit" ref="fruit1"></property>
    </bean>

或者可以直接创建一个内部Fruit类bean

    <bean id="person2" class="com.gisxx.beans.Person">
        <property name="name" value="张三"></property>
        <property name="age" value="22"></property>
        <property name="fruit">
            <bean class="com.gisxx.beans.Fruit" p:name="火龙果" p:price="12" />
        </property>
    </bean>

配置集合属性

在 Spring中可以通过一组内置的 xml 标签(例如: list, set 或 map) 来配置集合属性.

  • 配置 java.util.List 类型的属性, 需要指定 list> 标签, 在标签里包含一些元素. 这些标签可以通过 指定简单的常量值, 通过 ref指定对其他 Bean 的引用. 通过bean 指定内置 Bean 定义. 甚至可以内嵌其他集合.
  • 数组的定义和 List 一样, 都使用 list
  • 配置 java.util.Set 需要使用 set 标签, 定义元素的方法与 List 一样.

例如,如果一个人喜欢多种水果,Person的fruit属性改为:

private List<Fruit> fruits;

我们可以在property配置fruits属性中添加list元素,然后通过子元素ref来添加喜欢的水果bean:

    <bean id="person1" class="com.gisxx.beans.Person">
        <property name="name" value="张三"></property>
        <property name="age" value="22"></property>
        <property name="fruits">
            <list>
                <ref bean="fruit1"/>
                <ref bean="fruit2"/>
                <ref bean="fruit3"/>
            </list>
        </property>
    </bean>


  • Java.util.Map 通过 map标签定义, map标签里可以使用多个 entry 作为子标签. 每个条目包含一个键和一个值.

如果将Person的fruit属性改为Map类型:

private Map<Integer,Fruit> fruits;
    <bean id="person1" class="com.gisxx.beans.Person">
        <property name="name" value="张三"></property>
        <property name="age" value="22"></property>
        <property name="fruits">
            <map>
                <entry key="1" value-ref="fruit1"></entry>
                <entry key="2" value-ref="fruit2"></entry>
                <entry key="3" value-ref="fruit3"></entry>
            </map>
        </property>
    </bean>

猜你喜欢

转载自blog.csdn.net/agisboy/article/details/73801651