b05-2 Spring依赖注入(精通SPring 4.x)

2018-08-10  本文已影响0人  brusion

本篇内容:

一、属性注入

1.1、属性注入实例

1.1.1、创建java实体类

public class SetterObject {
​
    private String color;
    private int size;
​
    public void getData() {
        System.out.println("SetterObject{" +
                "color='" + color + '\'' +
                ", size=" + size +
                '}');
    }
​
    public void setColor(String color) {
        this.color = color;
    }
​
    public void setSize(int size) {
        this.size = size;
    }
}

1.1.2、配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
​
    <bean id="setter" class="com.demo.setter.SetterObject">
        <property name="color" value="红色"/>
        <property name="size" value="4188"/>
    </bean>
​
</beans>
说明:
property name="color" value="红色"
注意:

1.1.3、测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/setter.xml"})
public class SetterObjectTest {
​
    @Autowired
    private SetterObject setter;
​
    @Test
    public void getBean() {
        setter.getData();
    }
​
}

1.2、java中属性命名规范

二、构造函数注入

定义java实体对象

public class ConstructorObject {
​
    private String desc;
    private String title;
    private int size;
​
    public ConstructorObject(String desc, int size) {
        this.desc = desc;
        this.size = size;
    }
​
    public ConstructorObject(String desc, String title, int size) {
        this.desc = desc;
        this.title = title;
        this.size = size;
    }
​
    public void getData() {
        System.out.println("ConstructorObject{" +
                "desc='" + desc + '\'' +
                ", title='" + title + '\'' +
                ", size=" + size +
                '}');
    }
}

2.1、按照参数名称方式注入

配置文件

    <!--按照参数名称方式-->
    <bean id="constructorObject_name" class="com.demo.constructor.ConstructorObject">
        <constructor-arg name="desc" value="test-desc-data"/>
        <constructor-arg name="title" value="test-title-data"/>
        <constructor-arg name="size" value="4188"/>
    </bean>
    name="desc":对应的参数名称
    value="test-title-data":对应的参数值

2.2、按照参数类型注入

配置文件

    <!--按照参数类型-->
    <bean id="constructorObject_type" class="com.demo.constructor.ConstructorObject">
        <constructor-arg type="java.lang.String" value="test-desc-type"/>
        <constructor-arg type="java.lang.String" value="test-title-type"/>
        <constructor-arg type="int" value="4188"/>
    </bean>
    type="int":对应的参数类型
    value="4188":对应的参数值

2.3、按照参数索引注入

    <!--按照参数索引-->
    <bean id="constructorObject_index" class="com.demo.constructor.ConstructorObject">
        <constructor-arg index="0" value="test-desc-index"/>
        <constructor-arg index="1" value="test-title-index"/>
        <constructor-arg index="2" value="4188"/>
    </bean>

说明

2.4、按照反射方式注入

1、构造函数

    public ConstructorObject(String desc, int size) {
        this.desc = desc;
        this.size = size;
    }

2、配置

    <!--按照反射方式-->
    <bean id="constructorObject_reflection" class="com.demo.constructor.ConstructorObject">
        <constructor-arg value="test-desc-reflection"/>
        <constructor-arg value="4188"/>
    </bean>

完整配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
​
    <!--按照参数名称方式-->
    <bean id="constructorObject_name" class="com.demo.constructor.ConstructorObject">
        <constructor-arg name="desc" value="test-desc-data"/>
        <constructor-arg name="title" value="test-title-data"/>
        <constructor-arg name="size" value="4188"/>
    </bean>
​
    <!--按照参数类型-->
    <bean id="constructorObject_type" class="com.demo.constructor.ConstructorObject">
        <constructor-arg type="java.lang.String" value="test-desc-type"/>
        <constructor-arg type="java.lang.String" value="test-title-type"/>
        <constructor-arg type="int" value="4188"/>
    </bean>
​
    <!--按照参数索引-->
    <bean id="constructorObject_index" class="com.demo.constructor.ConstructorObject">
        <constructor-arg index="0" value="test-desc-index"/>
        <constructor-arg index="1" value="test-title-index"/>
        <constructor-arg index="2" value="4188"/>
    </bean>
​
    <!--按照反射方式-->
    <bean id="constructorObject_reflection" class="com.demo.constructor.ConstructorObject">
        <constructor-arg value="test-desc-reflection"/>
        <constructor-arg value="4188"/>
    </bean>
​
​
</beans>

完整测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/constructor.xml"})
public class ConstructorObjectTest {
​
    @Autowired
    public ConstructorObject constructorObject_name;
    @Autowired
    public ConstructorObject constructorObject_type;
    @Autowired
    public ConstructorObject constructorObject_index;
    @Autowired
    public ConstructorObject constructorObject_reflection;
​
    @Test
    public void getBeanByName() {
        constructorObject_name.getData();
    }
​
    @Test
    public void getBeanByType() {
        constructorObject_type.getData();
    }
​
    @Test
    public void getBeanByIndex() {
        constructorObject_index.getData();
    }
​
    @Test
    public void getBeanByReflection() {
        constructorObject_reflection.getData();
    }
​
}

三、循环注入

1、java实体对象

public class ForeObjectA {
​
    private String desc;
    private ForeObjectB foreObjectB;
​
    public ForeObjectA(String desc, ForeObjectB foreObjectB) {
        this.desc = desc;
        this.foreObjectB = foreObjectB;
    }
​
    public void getData() {
        System.out.println("ForeObjectA{" +
                "desc='" + desc + '\'' +
                ", foreObjectB=" + foreObjectB +
                '}');
    }
}

2、java实体对象

public class ForeObjectB {
​
    private String title;
    private ForeObjectA foreObjectA;
​
    public ForeObjectB(String title, ForeObjectA foreObjectA) {
        this.title = title;
        this.foreObjectA = foreObjectA;
    }
​
    public void getData() {
        System.out.println( "ForeObjectB{" +
                "title='" + title + '\'' +
                ", foreObjectA=" + foreObjectA +
                '}');
    }
}

3、配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
​
    <bean id="foreA" class="com.demo.fore.ForeObjectA">
        <constructor-arg name="desc" value="test-desc"/>
        <constructor-arg name="foreObjectB" ref="foreB"/>
    </bean>
​
    <bean id="foreB" class="com.demo.fore.ForeObjectB">
        <constructor-arg name="title" value="test-title"/>
        <constructor-arg name="foreObjectA" ref="foreA"/>
    </bean>
​
</beans>

4、测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/resources/fore.xml"})
public class ForeObjectTest {
​
    @Autowired
    private ForeObjectB foreB;
​
    @Autowired
    private ForeObjectA foreA;
​
    @Test
    public void getBean() {
        foreA.getData();
        foreB.getData();
    }
}

5、解决方案:

四、工厂方法注入

java实体对象

public class CarObject {
​
    private String color;
    private String title;
​
    public String getColor() {
        return color;
    }
​
    public void setColor(String color) {
        this.color = color;
    }
​
    public String getTitle() {
        return title;
    }
​
    public void setTitle(String title) {
        this.title = title;
    }
​
    @Override
    public String toString() {
        return "CarObject{" +
                "color='" + color + '\'' +
                ", title='" + title + '\'' +
                '}';
    }
}

4.1、非静态工厂方法注入

    public CarObject createCar() {
        CarObject carObject = new CarObject();
        carObject.setColor("银灰色");
        carObject.setTitle("雷克萨斯");
        return carObject;
    }

配置文件

    <bean id="factory" class="com.demo.factory.CarFactory"/>
    <bean id="car" factory-method="createCar" factory-bean="factory"/>

说明:

    id="factory":定义工厂类
    factory-method="createCar":指定创建实体类(id="car")对应的工厂方法
    factory-bean="factory":指定创建实体类(id="car") 对应的工厂类   
    

4.2、静态工厂

    public static CarObject newCar() {
        CarObject carObject = new CarObject();
        carObject.setColor("银灰色");
        carObject.setTitle("雷克萨斯");
        return carObject;
    }

配置文件

    <!--静态工厂类:不需要创建工厂类对象-->
    <bean id="newCar" class="com.demo.factory.CarFactory" factory-method="newCar"/>
说明:
        class="com.demo.factory.CarFactory":对应的静态工厂类
        factory-method="newCar":工厂类对应的静态方法

五、注入方式选择

1、使用构造函数方式的优点

2、构造函数方式的缺点

代码地址

https://github.com/brusion/brusion-code/tree/master/demo-java/03%20-%20demo%20-%20spring/spring-study-4x/01-ioc/01-ioc-type
上一篇下一篇

猜你喜欢

热点阅读