Spring依赖注入方式,自动装配及自动装配特征

Spring依赖注入方式

  • 一、setter注入
    • 1.1简单类型
    • 1.2引用类型(基本数据类型与String)
  • 二、构造器注入
    • 1.1简单类型
    • 1.2引用类型(基本数据类型与String)
  • 三、依赖注入方式选择
  • 四、自动装配
    • 依赖自动装配特征
  • 总结

一、setter注入

  • 依赖注入,是IOC的一个方面,是个通常的概念,它有多种解释。这概念是说你不用创建对象,而只需要描述它如何被创建。你不在代码里直接组装你的组件和服务,但是要在配置文件里描述哪些组件需要哪些服务,之后一个容器(IOC容器)负责把他们组装起来。

1.1简单类型


  • 配置中使用property标签ref属性注入引用类型对象

<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="bookDao" class="com.practice.dao.impl.BookDaoImpl1"/>
    <bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
    <bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
        <property name="bookDao" ref="bookDao"/>
        <property name="userDao" ref="userDao"/>
    bean>
beans>
  • 在bean中定义引用类型属性并提供可访问的set方法
package com.practice.service.impl;

import com.practice.dao.BookDao;
import com.practice.dao.UserDao;
import com.practice.dao.impl.BookDaoImpl1;
import com.practice.service.BookService;

/**
 * @Author YJ
 * @Date 2023/7/25 11:28
 * Description:
 */
public class BookServiceImpl implements BookService{
    private BookDao bookDao;
    private UserDao userDao;

    public void setBookDao(BookDaoImpl1 bookDao) {
        this.bookDao=bookDao;
    }
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void save() {
        System.out.println("bookService save...");
        bookDao.save();
        userDao.save();
    }


}

package com.practice;

import com.practice.dao.BookDao;
import com.practice.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author YJ
 * @Date 2023/7/26 11:32
 * Description:
 */
public class AppForDiSet {
    public static void main(String[] args) {
        //获取IoC容器
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) act.getBean("bookService");
        bookService.save();
    }
}


1.2引用类型(基本数据类型与String)


  • 配置中使用property标签value属性注入简单数据类型

<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="bookDao" class="com.practice.dao.impl.BookDaoImpl1">
        <property name="databaseName" value="mysql"/>
        <property name="connectionNum" value="5"/>
    bean>
    <bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
    <bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
        <property name="bookDao" ref="bookDao"/>
        <property name="userDao" ref="userDao"/>
    bean>
beans>
  • 在bean中定义引用类型属性并提供可访问的set方法
package com.practice.dao.impl;

import com.practice.dao.BookDao;

/**
 * @Author YJ
 * @Date 2023/7/26 11:24
 * Description:
 */
public class BookDaoImpl1 implements BookDao {
    private int connectionNum;
    private String databaseName;

    public void setConnectionNum(int connectionNum) {
        this.connectionNum = connectionNum;
    }

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    public void save() {
        System.out.println("bookDao save~" + databaseName + "," + connectionNum);
    }
}

package com.practice;

import com.practice.dao.BookDao;
import com.practice.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author YJ
 * @Date 2023/7/26 11:40
 * Description:
 */
public class AppForDiSet {
    public static void main(String[] args) {
        //获取IoC容器
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) act.getBean("bookService");
        bookService.save();
    }
}


Spring依赖注入方式,自动装配及自动装配特征_第1张图片

二、构造器注入

1.1简单类型


<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="bookDao" class="com.practice.dao.impl.BookDaoImpl1"/>
    <bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
    <bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
        <constructor-arg name="bookDao" ref="bookDao"/>
        <constructor-arg name="userDao" ref="userDao"/>
    bean>
beans>
package com.practice.service.impl;

import com.practice.dao.BookDao;
import com.practice.dao.UserDao;
import com.practice.dao.impl.BookDaoImpl1;
import com.practice.service.BookService;


/**
 * @Author YJ
 * @Date 2023/7/25 11:28
 * Description:
 */
public class BookServiceImpl implements BookService{
    private BookDao bookDao;
    private UserDao userDao;

    /**
     * 构造方法
     * @param bookDao
     * @param userDao
     */
    public BookServiceImpl(BookDao bookDao, UserDao userDao) {
        this.bookDao = bookDao;
        this.userDao = userDao;
    }

    public void save() {
        System.out.println("bookService save...");
        bookDao.save();
        userDao.save();
    }


}

public class AppForDiSet {
    public static void main(String[] args) {
        //获取IoC容器
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) act.getBean("bookService");
        bookService.save();
    }
}

Spring依赖注入方式,自动装配及自动装配特征_第2张图片

1.2引用类型(基本数据类型与String)



<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="bookDao" class="com.practice.dao.impl.BookDaoImpl1">
        <constructor-arg name="databaseName" value="mysql"/>
        <constructor-arg name="connectionNum" value="56"/>
    bean>
    <bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
    <bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
        <constructor-arg name="bookDao" ref="bookDao"/>
        <constructor-arg name="userDao" ref="userDao"/>
    bean>
beans>
package com.practice.dao.impl;

import com.practice.dao.BookDao;

/**
 * @Author YJ
 * @Date 2023/7/25 11:24
 * Description:
 */
public class BookDaoImpl1 implements BookDao {
    private int connectionNum;
    private String databaseName;

    /**
     * 构造方法
     * @param connectionNum
     * @param databaseName
     */
    public BookDaoImpl1(int connectionNum, String databaseName) {
        this.connectionNum = connectionNum;
        this.databaseName = databaseName;
    }

    public void save() {
        System.out.println("bookDao save~" + databaseName + ", " + connectionNum);
    }
}

public class AppForDiSet {
    public static void main(String[] args) {
        //获取IoC容器
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) act.getBean("bookService");
        bookService.save();
    }
}

Spring依赖注入方式,自动装配及自动装配特征_第3张图片


    <bean id="bookDao" class="com.practice.dao.impl.BookDaoImpl1">
        <constructor-arg type="java.lang.String" value="mysql"/>
        <constructor-arg type="int" value="56"/>
    bean>
    <bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
    <bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
        <constructor-arg name="bookDao" ref="bookDao"/>
        <constructor-arg name="userDao" ref="userDao"/>
    bean>

    <bean id="bookDao" class="com.practice.dao.impl.BookDaoImpl1">
        <constructor-arg index="1" value="mysql"/>
        <constructor-arg index="0" value="516"/>
    bean>
    <bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
    <bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
        <constructor-arg name="bookDao" ref="bookDao"/>
        <constructor-arg name="userDao" ref="userDao"/>
    bean>

三、依赖注入方式选择

  • 1.强制依赖使用构造器进行,使用setter注入有概率不进行注入导致null对象出现
  • 2.可选择依赖使用setter注入进行,灵活性强
  • 3.Spring框架倡导使用构造器,第三方框架内部大多数采用构造器注入的形式进行数据初始化,相对严谨
  • 4.如果有必要可以两者同时使用,使用构造器注入完成强制依赖的注入,使用setter注入完成可选依赖的注入
  • 5.实际开发过程中还要根据实际情况分析,如果受控对象没有提供setter方法就必须使用构造器注入
  • 6.自己开发的模块推荐使用setter注入

四、自动装配

  • IoC容器根据bean所依赖的资源在容器中自动查找并注入到bean的过程称为自动装配
  • 方式:
    • 按类型
    • 按名称
    • 按构造方法
    • 不启用自动装配
<bean id="bookDao" class="com.practice.dao.impl.BookDaoImpl2"/>
    <bean id="bookService" class="com.practice.service.impl.BookServiceImpl1" autowire="byType"/>
public class BookDaoImpl2 implements BookDao {

    public void save() {
        System.out.println("bookDao save~" );
    }
}
public class BookServiceImpl1 implements BookService {
    private BookDao bookDao;

    public void save() {
        System.out.println("bookService save...");
        bookDao.save();
    }


    public void setBookDao(BookDaoImpl2 bookDao) {
        this.bookDao = bookDao;
    }
}
public class AppForDiSet {
    public static void main(String[] args) {
        //获取IoC容器
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) act.getBean("bookService");
        bookService.save();
    }
}

Spring依赖注入方式,自动装配及自动装配特征_第4张图片

<bean id="bookDao" class="com.practice.dao.impl.BookDaoImpl2"/>
    <bean id="bookService" class="com.practice.service.impl.BookServiceImpl1" autowire="byName"/>
public class BookServiceImpl1 implements BookService {
    private BookDao bookDao;

    public void save() {
        System.out.println("bookService save...");
        bookDao.save();
    }


    public void setBookDao(BookDaoImpl2 bookDao) {
        this.bookDao = bookDao;
    }
}

Spring依赖注入方式,自动装配及自动装配特征_第5张图片

依赖自动装配特征

  • 自动装配用于引用类型依赖注入,不能对简单类型进行操作
  • 使用按类型装配时必须保证容器中相同类型的bean值唯一,推荐使用
  • 使用按名称装配时必须保证容器中具有指定名称的bean,因变量名与配置耦合,不推荐使用
  • 自动装配优先级低于setter注入与构造器注入,同时出现时自动装配失效

总结

通过这篇文章我们掌握了几个比较方便的注入方式,这在之后的注解开发会经常用到,欢迎各位点赞+关注!!!

你可能感兴趣的:(#,JavaWeb,spring,java,后端)