1. 程式人生 > >spring的屬性注入和構造器注入

spring的屬性注入和構造器注入

spring在向IOC容器中注入Bean的時候,有三種注入方式:

  • 屬性注入
  • 構造器注入
  • 工廠方法注入
    平常中用到的前兩種方法較多,下面對前兩種方法舉例。

一、屬性注入

1、建立一個car類,作為注入的bean

package com.lzj.spring;
public class Car {

    private String brand;
    private float price;

    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this
.brand = brand; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } @Override public String toString() { return "Car [brand=" + brand + ", price=" + price + "]"; } }

注意:如果要把定義的類通過xml的屬性注入IOC容器中,定義的類一定不要定義有參的構造器,否則會提示沒有預設的無參構造器錯誤。屬性注入是通過Car類的set方法進行注入的,因此一定要對類的屬性定義set方法。
2、配置bean.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
>
<bean id="car" class="com.lzj.spring.Car"> <property name="brand" value="baoma"></property> <property name="price" value="100000"></property> </bean> </beans>

bean標籤中的class一定要寫成全類名的形式,因為spring是根據類的全類名進行反射獲取類的。在進行屬性注入的時候,由於Car類有兩個屬性brand和price,所以定義兩個property標籤,name分別為Car類的兩個屬性,值分別為初始化屬性的值。通過屬性注入後,在IOC容器中就有了一個Bean為car的bean,即IOC容器中有一個Car類的例項car,例項的兩個屬性值分別為baoma和100000。
3、測試類

package com.lzj.springtest;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lzj.spring.Car;

public class SpringTest {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext ctx = 
                new ClassPathXmlApplicationContext("bean.xml");

        /*1、在容器中可以通過bean的id名字獲取bean,但是獲取到的bean預設為object型別,需要轉型為Car型別*/
        //Car car = (Car) ctx.getBean("car");
        /*2、在容器中也可以通過類的名字獲取bean,但前提是容器只有一個指定類的bean,否則出錯:沒有唯一的bean*/
        Car car = ctx.getBean(Car.class);
        System.out.println(car);
    }

}

經測試,Console中輸出:
Car [brand=baoma, price=100000.0]

二、構造器注入

1、建立Person類

package com.lzj.spring;
public class Person {

    private String name;
    private int age;
    private String sex;
    public Person(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    }

}

由於需要把Person類進行構造器注入到IOC容器中,所以不需要定義set方法,但要定義有參構造器,因為spring是通過Person的構造器進行初始化的。
2、配置bean.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="person" class="com.lzj.spring.Person">
        <!--注意constuctor的順序是按Person類的構造器的初始化順序-->
        <constructor-arg value="lzj"></constructor-arg>
        <constructor-arg value="20"></constructor-arg>
        <constructor-arg value="female"></constructor-arg>
    </bean>

</beans>

在進行構造器注入的時候,也可以寫成

<bean id="person" class="com.lzj.spring.Person">
        <constructor-arg value="lzj" index="0"></constructor-arg>
        <constructor-arg value="20" index="1"></constructor-arg>
        <constructor-arg value="female" index="2"></constructor-arg>
    </bean>

用index來定義構造器中引數的順序。也可以通過屬性的型別進行定義。例如:

    <bean id="person" class="com.lzj.spring.Person">
        <constructor-arg value="lzj" type="java.lang.String"></constructor-arg>
        <constructor-arg value="20" type="int"></constructor-arg>
        <constructor-arg value="female" index="2"></constructor-arg>
    </bean>

也可以通過屬性名進行注入值,例如:

<bean id="person" class="com.lzj.spring.Person">
        <constructor-arg value="lzj" type="java.lang.String"></constructor-arg>
        <constructor-arg value="20" type="int"></constructor-arg>
        <!--也可以通過屬性名進行注入值-->
        <constructor-arg name="sex" value="female"></constructor-arg>
    </bean>

3、測試類

public class SpringTest {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext ctx = 
                new ClassPathXmlApplicationContext("bean.xml");
        Person person = ctx.getBean(Person.class);
        System.out.println(person);
    }

}

輸出內容為:

Person [name=lzj, age=20, sex=female]

以上即為屬性輸入和構造器注入的區別。