Spring中使用构造函数进行依赖注入

基于构造函数的依赖注入:
使用的标签:constructor-arg
标签出现的位置:bean标签的内部
标签的属性:

  • type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型。
  • index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始。
  • name:用于指定给构造函数中指定名称的参数赋值。(常用)
  • value:用于提供基本类型和String类型的数据
  • ref:用于指定其他的bean类型数据,它指的就是在spring的IoC核心容器中出现过的bean对象。

特点(优势):在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
弊端:改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据也必须提供。

示例:
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.qublog</groupId>
    <artifactId>spring01_di</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

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

</project>

AccountService接口:

package com.qublog.service;

//账户业务层接口
public interface AccountService {
    //模拟保存账户
    void saveAccount();
}

AccountServiceImpl类:

package com.qublog.service.impl;

import com.qublog.service.AccountService;

import java.util.Date;

//账户的业务层实现类
public class AccountServiceImpl implements AccountService {

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

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

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

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">
    <!-- 把对象的创建交给spring来管理 -->
    <!-- 构造函数注入:
        使用的标签:constructor-arg
        标签出现的位置:bean标签的内部
        标签的属性
    -->
    <bean id="accountService" class="com.qublog.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>

Client类:

package com.qublog.ui;

import com.qublog.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//模拟一个表现层,用于调用业务层
public class Client {
    public static void main(String[] args) {
        //获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //根据id获取bean对象,下面两种方式都可以
        AccountService as = (AccountService)ac.getBean("accountService");
        as.saveAccount();
    }
}

打印结果:
在这里插入图片描述

发布了56 篇原创文章 · 获赞 0 · 访问量 530

猜你喜欢

转载自blog.csdn.net/qq_41242680/article/details/105611274