码迷,mamicode.com
首页 > 其他好文 > 详细

依赖注入的3种方式

时间:2017-11-02 21:14:45      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:ret   依赖   create   属性   oid   ice   nbsp   style   contex   

 

属性注入:

Car.class

public class Car {
    public String brand;
    private String corp;    
    private double price;
    private int maxSpeed;

public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String toString(){ return "brand:"+brand+"/maxSpeed:"+maxSpeed+"/price:"+price; } }

 

配置文件

<!-- 属性注入 -->
    <!--bean id="car" class="com.smart.ditype.Car">
        <property name="brand" value="红旗&amp;CA72"/>
        <property name="maxSpeed" value="200"/>
        <property name="price" value="20000.00"/>
    </bean>

测试:

public class DiTypeTest {
    public ApplicationContext factory = null;

    private static String[] CONFIG_FILES = { "com/smart/ditype/beans.xml" };

    @BeforeClass
    public void setUp() throws Exception {
        factory = new ClassPathXmlApplicationContext(CONFIG_FILES);
    }

    @Test
    public void testCar(){
        Car car = (Car)factory.getBean("car");
        assertNotNull(car);
        System.out.println(car);
    }
}

 

构造函数注入:

Car.class

public class Car {
    public String brand;
    private String corp;    
    private double price;
    private int maxSpeed;

    public Car() {}    
    public Car(String brand, double price) {
        this.brand = brand;
        this.price = price;
    }    

    public Car(String brand, String corp, double price) {
        this.brand = brand;
        this.corp = corp;
        this.price = price;
    }
    public Car(String brand, String corp, int maxSpeed) {
        this.brand = brand;
        this.corp = corp;
        this.maxSpeed = maxSpeed;
    }
}

 

配置文件:

<!--构造函数注入:type -->
    <bean id="car1" class="com.smart.ditype.Car">
        <constructor-arg type="java.lang.String">
            <value>红旗CCCA72</value>
        </constructor-arg>
        <constructor-arg type="double">
            <value>10000</value>
        </constructor-arg>
    </bean>

测试:

@Test
    public void testCar1(){
        Car car1 = (Car)factory.getBean("car1");
        assertNotNull(car1);
        System.out.println(car1);
    }    

 

工厂注入:

CarFactory.class

public class CarFactory {
   public Car createHongQiCar(){
       Car car = new Car();
       car.setBrand("红旗CA72");
       return car;
   }
   
   public static Car createCar(){
       Car car = new Car();
       return car;
   }
}

配置文件:

<!-- 工厂方法-->
    <bean id="carFactory" class="com.smart.ditype.CarFactory" />
<bean id="car5" factory-bean="carFactory" factory-method="createHongQiCar">
</bean>
<bean id="car6" class="com.smart.ditype.CarFactory"
factory-method="createCar"></bean>

测试:

@Test
    public void testCar5(){
        Car car5 = (Car)factory.getBean("car5");
        assertNotNull(car5);
        System.out.println("car5:"+car5);
    }

    @Test
    public void testCar6(){
        Car car6 = (Car)factory.getBean("car6");
        assertNotNull(car6);
        System.out.println("car6:"+car6);
    }

 

依赖注入的3种方式

标签:ret   依赖   create   属性   oid   ice   nbsp   style   contex   

原文地址:http://www.cnblogs.com/lwx521/p/7774187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!