Spring依赖注入-----构造函数注入

IDEA中新建maven项目

整体架构

在这里插入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.uek</groupId>
    <artifactId>spring01-study02-DI</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.12.RELEASE</version>
        </dependency>
    </dependencies>

</project>

service


public interface IAccountService {

    //保存账户
    void saveAccount();
}

public class AccountServiceImpl implements IAccountService {

    //如果是经常变化的数据,并不适合注入的方式
    private String name;
    private Integer age;
    private Date birthday;

    public AccountServiceImpl(String name, Integer age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public void saveAccount() {
        System.out.println("service中的saveAccount方法执行了..."+name+","+age+","+birthday);
    }

}

ui

/**
 * 表现层,用于业务调用
 */
public class Client {

    //获取Spring的IOC核心容器,并根据id获取对象
    public static void main(String[] args) {
        //1.获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id获取bean对象
        IAccountService ias = (IAccountService) ac.getBean("accountService");

        ias.saveAccount();
    }
}

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 id="accountService" class="com.uek.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>

    <!--配置一个日期对象-->
    <bean id="now" class="java.util.Date"></bean>

</beans>

小笔记

构造函数注入:
        使用的标签:constructor-arg
        标签出现的位置:bean标签内部
        标签中的属性:
             type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型。
             index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始的。
             name:用于指定给构造函数中指定名称的参数赋值     常用的
             ====================以上三个用于指定给构造函数中哪个参数赋值=============================
             value:用于提供基本类型和String类型的数据
             ref:用于指定其他的bean类型数据,指的就是在spring的IOC核心容器中出现的bean对象
       优势:
            在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
       弊端:
            改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值