Spring基础-DI依赖注入有参构造注入bean

DI注入属性及管理bean之间的依赖关系(有参构造)

目标:
通过bean的有参构造创建对象并为属性赋值
实例化DogEntity及CatEntity,将DogEntity注入到CatEntity,并调用DogEntity中dog()方法

代码

1、创建DogEntity及CatEntity两个类
package work.chenc.entity;

public class DogEntity {

    private String dname;

    private String dcolor;

    public DogEntity() {
        System.out.println("DogEntity执行了无参构造--------------------");
    }

    public DogEntity(String dname, String dcolor) {
        System.out.println("DogEntity执行了有参构造。。。。。。。。。。。");
        this.dname = dname;
        this.dcolor = dcolor;
    }

    public void dog() {
        System.out.println("小狗名叫:" + dname + "颜色:" + dcolor);
    }

}
package work.chenc.entity;

public class CatEntity {

    private DogEntity dogEntity;

    public CatEntity() {
        System.out.println("CatEntity执行了无参构造--------------------");
    }

    public CatEntity(DogEntity dogEntity) {
        System.out.println("CatEntity执行了有参构造。。。。。。。。。。。");
        this.dogEntity = dogEntity;
    }

    public void doSmoething() {
        System.out.println("doSmoething.................");
        dogEntity.dog();
    }


}

2、pom.xml

<dependencies>
        <!-- junit测试包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- spring-context 包含Spring-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>

    </dependencies>

3、创建bean.xml文件

<?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">

    <!-- 创建CatEntity对象 并将 DogEntity 注入到 CatEntity-->
    <bean id="cat" class="work.chenc.entity.CatEntity">
        <constructor-arg name="dogEntity" ref="dog" />
    </bean>

    <bean id="dog" class="work.chenc.entity.DogEntity">
           <!--
                <constructor-arg index="0" value="局长"/>
                <constructor-arg index="2" value="黑色"/>
                上述这种通过index去找到的对应的属性并赋值,顺序是从0开始——对应bean构造方法第一个参数开始
                0 ===> dname        1 ===> dcolor
           -->
           <constructor-arg name="dname" value="局长"/>
           <constructor-arg name="dcolor" value="黑色"/>
       </bean>
</beans>
4、Junit测试
@Test
public void testDog() {
    // 1、获取ApplicationContent对象解析xml文件并创建对象 - spring内部处理
    // 我这里bean2.xml 是我对应的xml文件的名字 也就是上面bean.xml
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean3.xml");
    // 2、获取创建的对象
    CatEntity carEntity = context.getBean("cat", CatEntity.class);
    // 2、执行UserEntity中doSomething方法 doSomething 再调用 BookEntity 中的 outPut() 方法
    carEntity.doSmoething();
}

执行结果

DogEntity执行了有参构造。。。。。。。。。。。
CatEntity执行了有参构造。。。。。。。。。。。
doSmoething.................
小狗名叫:局长颜色:黑色

通过以上执行结果可以看出
1、DogEntity及CatEntity无论是实例化还是属性赋值都是调用的无参构造
结合之前无参构造可以看出他们之间的区别是xml配置的文件的区别,无参构造通过< property/>赋值,有参构造通过<constructor-arg/>赋值

(个人理解:IOC通过bean的构造方法创建对象, DI通过属性的set方法赋值及管理bean依赖或通过有参构造注入属性值及管理bean依赖关系)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容