[Spring] class extends 關係的 xml 設定

當class有繼承關係的時候,spring的設定方式,需表示的java code為:
package example.Vehicle;
public class Vehicle {

    protected String type = "vehicle"; 
    
    public void printType() {
        System.out.println("This is a " + type + ".");
    }
}

package example.Bus;

public class Bus extends Vehicle {

     public Bus() {
         type = "bus";
     }
}

package example.Trunk;

public class Trunk extends Vehicle {

    public Trunk() {
        type = "trunk";
    }
}
xml的表示方式為
<bean id="vehicle" class="example.Vehicle" />

<bean id="bus" class="example.Bus" parent="vehicle" />

<bean id = "trunk" class="example.Trunk" parent="vehicle" />

沒有留言:

張貼留言