【Spring基础】IOC使用构造器依赖注入

前言

Github:https://github.com/yihonglei/thinking-in-spring(spring-ioc-xml工程)

一 构造器注入bean

1、创建HelloService接口

package com.jpeony.spring.common;

/**
 * @author yihonglei
 */
public interface HelloService {
    void sayHello(String name);
}

2、HelloServiceImpl实现类

package com.jpeony.spring.common;

/**
 * @author yihonglei
 */
public class HelloServiceImpl implements HelloService {

    @Override
    public void sayHello(String name) {
        System.out.println("hello:" + name);
    }
}

3、SelfIntroductionService(自我介绍)接口

package com.jpeony.spring.constructor;

/**
 * @author yihonglei
 */
public interface SelfIntroductionService {
    void selfIntroduction();
}

4、SelfIntroductionServiceImpl实现类

package com.jpeony.spring.constructor;

import com.jpeony.spring.common.HelloService;

/**
 * @author yihonglei
 */
public class SelfIntroductionServiceImpl implements SelfIntroductionService {
    private HelloService helloService;

    /**
     * 构造器注入Bean
     */
    public SelfIntroductionServiceImpl(HelloService helloService) {
        this.helloService = helloService;
    }

    @Override
    public void selfIntroduction() {
        // 向大家打招呼
        helloService.sayHello("大家好!");
    }

}

5、Spring xml配置applicationContext-Constructor-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">
    <!--
       Bean声明:
         该bean类似于javaConfig中的@Bean注解;
         用于创建bean的类通过class属性来指定,并且需要使用全限定的类名。
         通过id指定bean的ID。如果不显示指定,默认使用class的全限定名进行命名。
         eg:
         HelloServiceImpl#0,其#0是一个计数器的形式,
         用来区分相同类型的其他bean。
         使用自动化命名很方便,但是没有多少实际用处,还是建议自己给bean显示设定ID。
     -->
    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>

    <!-- 构造器注入bean -->
    <bean id="selfIntroductionService" class="com.jpeony.spring.constructor.SelfIntroductionServiceImpl">
        <!-- 构造器注入bean -->
        <constructor-arg ref="helloService"/>
    </bean>

</beans>

6、测试构造器注入bean

package com.jpeony.spring;

import com.jpeony.spring.constructor.SelfIntroductionService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试构造器注入(bean、字面量)
 *
 * @author yihonglei
 */
public class ConstructorTest {

    /**
     * 造器注入Bean
     *
     * @author yihonglei
     */
    @Test
    public void testBean() {
        // 根据spring配置文件创建应用上下文
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext-Constructor-Bean.xml");
        // 从容器中获取bean
        SelfIntroductionService selfIntroductionService
                = (SelfIntroductionService) context.getBean("selfIntroductionService");
        // 调用自我介绍
        selfIntroductionService.selfIntroduction();
    }

}

7、运行结果

hello:大家好!

二 构造器注入字面量

1、创建一个实体类

package com.jpeony.spring.entity;

/**
 * 个人信息(构造器注入使用)
 *
 * @author yihonglei
 */
public class PersonConstructor {

    /**
     * 姓名
     */
    private String name;
    /**
     * 性别
     */
    private String sex;
    /**
     * 年龄
     */
    private int age;
    /**
     * 兴趣爱好
     */
    private String hobby;

    /**
     * 构造器注入时打开构造器--setter注入时注释掉构造器
     *
     * @param name
     * @param sex
     * @param age
     * @param hobby
     */
    public PersonConstructor(String name, String sex, int age, String hobby) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}

2、Spring xml配置applicationContext-Constructor-Constant.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">
    <!-- 构造器注入字面量 -->
    <bean id="personConstructor" class="com.jpeony.spring.entity.PersonConstructor">
        <!-- 第一种方式: 根据参数索引位置匹配 -->
        <constructor-arg index="0" value="tom"/>
        <constructor-arg index="1" value="男"/>
        <constructor-arg index="2" value="26"/>
        <constructor-arg index="3" value="爱好女人,哈哈!开个玩笑!"/>
        <!-- 第二种方式: 根据参数类型匹配 -->
        <!--<constructor-arg value="tom" type="java.lang.String"/>
        <constructor-arg value="男" type="java.lang.String"/>
        <constructor-arg value="26" type="int"/>
        <constructor-arg value="爱好女人,哈哈!开个玩笑!" type="java.lang.String"/>-->
    </bean>

</beans>

3、测试构造器注入字面量

package com.jpeony.spring;

import com.jpeony.spring.entity.PersonConstructor;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试构造器注入(bean、字面量)
 *
 * @author yihonglei
 */
public class ConstructorTest {

    /**
     * 造器注入常量
     *
     * @author yihonglei
     */
    @Test
    public void testConstant() {
        // 根据spring配置文件创建应用上下文
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext-Constructor-Constant.xml");
        // 从容器中获取bean
        PersonConstructor person = (PersonConstructor) context.getBean("personConstructor");
        // 打印个人属性
        System.out.println(" 姓名: " + person.getName() +
                " 性别: " + person.getSex() +
                " 年龄: " + person.getAge() +
                " 兴趣爱好: " + person.getHobby());
    }

}

4、运行结果

姓名: tom 性别: 男 年龄: 26 兴趣爱好: 爱好女人,哈哈!开个玩笑!

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值