Dependency injection using constructor in Spring

Based constructor dependency injection:
Label used: constructor-arg
location tag appear: internal bean tag
label attribute:

  • type: Used to specify the data type of the data to be injected, which is also the type of one or some parameters in the constructor.
  • index: Used to specify the data to be injected to the parameter of the specified index position in the constructor. The index position starts from 0.
  • name: used to assign a value to the parameter of the specified name in the constructor. (Commonly used)
  • value: used to provide basic type and String type data
  • ref: used to specify other bean type data, it refers to the bean object that has appeared in the spring IoC core container.

Features (advantages): When acquiring bean objects, injecting data is a necessary operation, otherwise the object cannot be created successfully.
Disadvantages: Changed the instantiation of bean objects, so that when we create objects, we must provide them if we can't use the data.

Example:
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 interface:

package com.qublog.service;

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

AccountServiceImpl class:

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 class:

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();
    }
}

Print the result:
Insert picture description here

Published 56 original articles · liked 0 · visits 530

Guess you like

Origin blog.csdn.net/qq_41242680/article/details/105611274