Spring - 构造器注入和Setter注入引用类型和简单类型

167 阅读2分钟

Setter注入引用类型

1、定义引用类型属性和可访问的set方法

package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.service.BookService;


public class BookServiceImpl implements BookService {
    //Setter注入引用类型
    private BookDao bookDao;//在bean中定义引用类型属性
    @Override
    public void bookService() {
        System.out.println("BookService");
    }

    // 提供可访问的set方法
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }
}

2、配置中使用property标签 ref属性注入引用类型对象 name属性表示引用类型变量

<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>

<!--    配置中使用property标签
            ref属性注入引用类型对象
            name属性表示引用类型变量
        -->
    <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
        <property name="bookDao" ref="bookDao"/>
    </bean>
</beans>

Setter注入简单数据类型

1、定义简单类型属性和可访问的set方法

package com.itheima.service.impl;

import com.itheima.service.BookService;


public class BookServiceImpl implements BookService {
    //Setter注入简单类型
    private int age;//在bean中定义引用类型属性
    private String name;
    @Override
    public void bookService() {
        System.out.println("BookService");
    }

    //提供可访问的set方法
    public void setAge(int age) {
        this.age = age;
    }

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

2、配置中使用property标签,name属性表示引用类型变量,value属性表示变量的值,有几个变量就定义几个property

<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>

<!--    配置中使用property标签
            name属性表示引用类型变量
            value属性表示变量的值
            有几个变量就定义几个property
        -->
    <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
            <property name="name" value="张三"/>
            <property name="age" value="18"/>
     </bean>
</beans>

构造器注入简单数据类型

1、定义简单类型属性和可访问的构造方法

package com.itheima.service.impl;

import com.itheima.service.BookService;


public class BookServiceImpl implements BookService {
    //构造器注入简单类型
    private int age;//在bean中定义简单类型属性
    private String name;
    @Override
    public void bookService() {
        System.out.println("BookService" + " " +  name + " " + age);
    }

    //提供可访问的构造方法
    public BookServiceImpl(int age, String name) {
        this.age = age;
        this.name = name;
    }
}

2、 配置中使用constructor-arg标签name属性表示引用类型变量,value属性表示变量的值,有几个变量就定义几个constructor-arg

<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>

<!--    配置中使用constructor-arg标签
            name属性表示引用类型变量
            value属性表示变量的值
            有几个变量就定义几个constructor-arg
        -->
    <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
      <constructor-arg name="name" value="张三"/>
      <constructor-arg name="age" value="18"/>
     </bean>
</beans>

构造器注入引用数据类型

1、定义引用类型属性和可访问的构造方法

package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.service.BookService;


public class BookServiceImpl implements BookService {
    //构造器注入引用类型
    private BookDao bookDao;//在bean中定义引用类型属性

    @Override
    public void bookService() {
        System.out.println("BookService");
        bookDao.bookDao();
    }

    //提供可访问的构造方法
    public BookServiceImpl(BookDao bookDao) {
        this.bookDao = bookDao;
    }
}

2、 配置中使用constructor-arg标签 ,name属性表示构造器的形参 ,ref属性表示注入引用类型对象,有几个变量就定义几个constructor-arg

<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>

<!--    配置中使用constructor-arg标签
            name属性表示构造器的形参
            ref属性表示注入引用类型对象
            有几个变量就定义几个constructor-arg
        -->
    <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
    <constructor-arg name="bookDao" ref="bookDao"/>
     </bean>
</beans>

构造器注入-参数适配

image.png

依赖注入方式选择

1、强制依赖使用构造器注入,使用Setter注入有概率不进行注入导致null对象出现

2、可选依赖使用Setter注入进行,灵活性强

3、Spring框架倡导使用构造器注入,第三方框架大多数采用构造器注入的形式进行数据初始化,相对严谨

4、如果有必要可以两者同时使用,使用构造器注入完成强制依赖的注入,使用Setter注入完成可选依赖的注入

5、实际开发中还是根据实际情况分析,如果受控对象没有提供Setter方法就必须是构造器注入

6、自己开发的模块推荐使用Setter注入