• 依赖注入的3种方式


     

    属性注入:

    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);
        }
  • 相关阅读:
    Memcached缓存在.Net 中的使用(memcacheddotnet)
    转载 单目摄像机标定说明
    图像的上采样 下采样
    VOC数据集 目标检测
    tensorflow用pretrained-model做retrain
    图像分类基础
    TensorRT学习总结
    Jetson Nano Developer Kit
    pytorch深度学习60分钟闪电战
    系统检测工具ROSWTF
  • 原文地址:https://www.cnblogs.com/lwx521/p/7774187.html
Copyright © 2020-2023  润新知