spring(2)-配置bean及属性

    上节中我们通过maven构建项目,并完成了第一个spring的hello world, 下边我们继续学习spring的bean属性配置。

1. 通过set方法配置bean

    1.1 创建javaBean,我们还是使用上一讲中的Car对象

public class Car {
    private String name;
    private double price;
    public Car(){

    }
    public Car(String name,double price){
        this.name = name;
        this.price = price;
        System.out.println(name+":"+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 "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
    1.2. 配置bean

<!--set 方法注入bean-->
<bean id="car1" class="com.spring.demo.entity.Car">
    <property name="name" value="宝马"/>
    <property name="price" value="600000"/>
</bean>

    1.3. 在ioc容器即可获得car1对象

    说明:注意其中使用的几个属性

            scope:表示创建对象的形式。

                  prototype:表示在加载ioc容器时,不会创建对象,只有使用对象时才会创建对象,且对象创建为多例。

                  singleton:表示在加载doc容器时,就会创建对象,且对象为单例。

2. 通过构造方法配置bean

    对象我们还是使用上边的bean,但配置文件如下

<!--构造器注入-->
<bean id="car2" class="com.spring.demo.entity.Car">
    <constructor-arg name="name" value="奥迪"/>
    <constructor-arg name="price" value="500000"/>
</bean>

    构造器注入通过constructor-arg来进行构造函数参数传递,其中我们需要注意其中的几个属性

    1. index:表示参数的下标,值为0,1,2等

    2.type:表示参数的类型,根据对象中的构造函数进行配置

3. 对象间的引用配置

<!--对象间依赖注入-->
<bean id="person1" class="com.spring.demo.entity.Person">
    <property name="name" value="Tom"/>
    <property name="age" value="18"/>
    <!--对象间注入通过ref进行注入-->
    <property name="car" ref="car1"/>
</bean>

    对象间引用是通过ref进行配置的,name为对象的对象类型,ref为对应的对象id 

4. list集合配置

<!--list集合-->
<bean id="person2" class="com.spring.demo.entity.Person">
    <!--注入car集合,需要使和list与ref标签进行组合使用-->
    <property name="carList">
        <list value-type="com.spring.demo.entity.Car">
          <ref bean = "car1"/>
            <ref bean="car2"/>
        </list>
    </property>
    <property name="name" value="Jeck"/>
    <property name="age" value="20"/>
    <!--如果需要注入null值,则需要使用null标签-->
    <property name="car"><null/></property>
</bean>

        list集合进行配置时,name为集合在对象中的属性名,value-type:表示list集合中对象的类型,如果不配置则黙认为string.

        如果list集合中为对象,则通过ref进行配置,如果为其它类型则根据<value> 进行配置。

        如果要配置对象为空,不能使用value="null",这样会使值为null字符串,而应该使用<null/>进行配置

        set集合与list集合一样的配置

    5.map集合配置

<!--set方法与list用法一致-->
<bean id="person3" class="com.spring.demo.entity.Person">
    <property name="name" value="lilei"/>
    <property name="age" value="22"/>
    <property name="car"><null/></property>
    <property name="carList">
        <list value-type="com.spring.demo.entity.Car">
            <null></null>
        </list>
    </property>
    <property name="carMap">
        <map>
            <entry key="car1" value-ref="car1"/>
            <entry key="car2" value-ref="car2"/>
        </map>

    </property>
</bean>

        map集合其它属性与list保持一致,但需要通过<map>进行配置,然后通过entry进行配置,具体配置如上。

    6.properties文件读取和properties属性配置

<!--properties读取,使用频率较高,主要用于读取配置文件-->
<!--读取配置文件-->
<context:property-placeholder location="config.properties"/>

<bean id="dataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="password" value="${password}"/>
    <property name="user" value="${username}"/>
    <property name="jdbcUrl" value="${jdbcUrl}"/>
</bean>

<!--直接读取bean中定义的properties-->
<bean id="person4" class="com.spring.demo.entity.Person">
    <property name="properties">
        <props>
            <prop key="1">小明</prop>
            <prop key="2">小张</prop>
            <prop key="2">小花</prop>
        </props>
    </property>
</bean>

    properties具体配置如上,其中如果要读了取配置文件,需要使用Context:property-placeholder进行读取文件内容,然后直接通过表达式进行读取文件内容:${属性名}进行读取

    7. 静态实例工厂方法

        7.1 创建静态实例方法

public class StaticCarFactory {

    public static Car getCar(String name,double price){
        return new Car(name,price);
    }

}

        7.2 配置静态工厂    

<bean id="car4" class="com.spring.demo.entity.StaticCarFactory" factory-method="getCar">
    <constructor-arg name="name" value="奥拓"/>
    <constructor-arg name="price" value="20000"/>
</bean>

        最后得到的即为汽车对象

    8. 实例工厂方法实例化bean

        8.1 创建实例工厂方法

public class CarMethod {

    public Car getCar(String name,double price){
        return new Car(name,price);
    }

}

        8.2 配置实例工厂方法实例化bean        

<bean id="carFactory" class="com.spring.demo.entity.CarMethod"></bean>
<bean id="car5" factory-bean="carFactory" factory-method="getCar">
    <constructor-arg name="name" value="奔驰"/>
    <constructor-arg name="price" value="60000"/>
</bean>

        通过此方法也可以获得对应的bean,以上即为通过配置形式可能获得对象的方式。

猜你喜欢

转载自blog.csdn.net/qq_37317845/article/details/80978599