1. 程式人生 > >屬性注入

屬性注入

目錄

set方式

User類

該類包含set方法

public class User {

    private String name;
    private String age;
    private Car car;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getAge() {
        return age;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public Car getCar() {
        return car;
    }

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

Car類

該類也包含set方法

public class Car {
    private String name;
    private String color;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}

applicationContext.xml

注意中的ref。

<bean id="user" class="com.me.injection.User">
	<property name="name" value="張三"></property>
	<property name="age" value="22"></property>
	<property name="car" ref="car"></property>
</bean>

<bean id="car" class="com.me.injection.Car">
	<property name="name" value="嗶嗶"></property>
	<property name="color" value="red"></property>
</bean>

建構函式方式

User類

public class User {

    private String name;
    private String age;
    private Car car;

  public User(){

  }

  public User(String name,String age){
      super();
      this.name=name;
      this.age=age;
  }

    public User(String name,String age,Car car){
        super();
        this.name=name;
        this.age=age;
        this.car=car;
    }

    public User(String name) {
    }

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

Car類

public class Car {
    private String name;
    private String color;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}

applicationContext.xml

<bean id="user" class="com.me.injection.User">
   <constructor-arg name="name" value="李四"></constructor-arg>
   <constructor-arg name="age" value="22"></constructor-arg>
   <constructor-arg name="car" ref="car"></constructor-arg>
</bean>
<bean id="car" class="com.me.injection.Car">
   <property name="name" value="嗶嗶"></property>
   <property name="color" value="red"></property>
</bean>