Spring-DI依赖注入

一、概述

在当前类需要用到其他类的对象时,由Spring为我们提供,我们只需要在配置文件中说明依赖关系的维护,这种方式就称为依赖注入

依赖注入:
能注入的数据有三类:

  1. 基本类型和String
  2. 其他Bean类型
  3. 复杂类型/集合类型

注入的方式有三种:

  1. 构造函数
  2. set方法
  3. 注解
二、注入方式之构造函数注入

使用的标签:constructor-arg
标签出现的位置:bean标签的内部
标签中的属性

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

优势:
在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。

弊端:
改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。

例:
业务层

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

bean.xml配置

<bean id="accountService" class="com.zg.service.impl.AccountServiceImpl">
      <constructor-arg name="name" value="test" ></constructor-arg>
      <constructor-arg name="age" value="18"></constructor-arg>
      <constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
二、注入方式之set方法注入(更常用)

涉及的标签:property
出现的位置:bean标签的内部
标签的属性

  • name:用于指定注入时所调用的set方法名称
  • value:用于提供基本类型和String类型的数据
  • ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象

优势:
创建对象时没有明确的限制,可以直接使用默认构造函数。

弊端:
如果有某个成员必须有值,则获取对象时有可能set方法没有执行。

1、基本类型和 String 的注入方式

例:
业务层

public class AccountServiceImpl implements IAccountService {
    private String name;
    private Integer age;
    private Date birthday;
    public void saveAccount() {
        System.out.println("service中的saveAccount方法执行了......"+name+","+age+","+birthday);
    }
    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

bean.xml

  <bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl">
        <property name="name" value="TEST" ></property>
        <property name="age" value="21"></property>
        <property name="birthday" ref="now"></property>
    </bean>
2、复杂集合类型的注入方式

用于给List结构集合注入的标签:list array set
用于个Map结构集合注入的标签:map props
结构相同,标签可以互换
例:
业务类

public class AccountServiceImpl2 implements IAccountService {
    private String[] myStrs;
    private List<String> myList;
    private Map<String,String> myMap;
    private Properties myProps;
    @Override
    public void saveAccount() {
        System.out.println(Arrays.toString(myStrs));
        System.out.println(myList);
        System.out.println(myMap);
        System.out.println(myProps);
    }

    public void setMyStrs(String[] myStrs) {
        this.myStrs = myStrs;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }

    public void setMyProps(Properties myProps) {
        this.myProps = myProps;
    }
}

bean.xml

<bean id="accountService3" class="com.zg.service.impl.AccountServiceImpl2">
        <property name="myStrs">
            <array>
                <value>AAA</value>
                <value>BBB</value>
            </array>
        </property>
        <property name="myList">
            <list>
                <value>AAA</value>
                <value>BBB</value>
            </list>
        </property>
        <property name="myMap">
            <map>
                <entry key="testA" value="AAA"></entry>
            </map>
        </property>
 </bean>
二、注入方式之注解注入

曾经XML的配置:

 <bean id="accountService" class="com.zg.service.impl.AccountServiceImpl"
   scope=""  init-method="" destroy-method="">
       <property name=""  value=""  ref=""></property>
 </bean>

1、用于创建对象
@Component:
他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的

作用:用于把当前类对象存入Spring容器中

属性:

  • value:用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母改小写。

@Controller:一般用在表现层
@Service:一般用在业务层
@Repository:一般用在持久层
以上三个注解他们的作用和属性与Component是一模一样。
他们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰

例:

@Component
public class AccountServiceImpl implements IAccountService {

  public AccountServiceImpl(){
      System.out.println("对象创建了");
  }

bean.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
    <!--告知Spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
    context名称空间和约束中-->
    <context:component-scan base-package="com.zg"></context:component-scan>
</beans>

2、用于注入数据
@Autowired:
他们的作用就和在xml配置文件中的bean标签中写一个<property>标签的作用是一样的

作用:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。

如果IoC容器中没有任何bean的类型和要注入的变量类型匹配,则报错。
如果IoC容器中有多个类型匹配时:先根据数据类型划定容器中匹配的范围,再根据变量名称匹配容器中bean的id,匹配到一样时,创建成功,否则报错。(所以这要求id一样)

出现位置:可以是变量上,也可以是方法上

细节:在使用注解注入时,set方法就不是必须的了。

@Qualifier:

  • 作用:在按照类中注入的基础之上再按照名称注入(配合Autowired一起使用)。它在给类成员注入时不能单独使用。但是在给方法参数注入时可以。

  • 属性:value:用于指定注入bean的id。

public class AccountServiceImpl implements IAccountService {

    @Autowired
    @Qualifier("accountDao1")//后面指明注入进哪个对象
    private IAccountDao accountDao = null;
    ........
    }

@Resource:

  • 作用:直接按照bean的id注入。它可以独立使用
  • 属性:name:用于指定bean的id。

以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现。另外,集合类型的注入只能通过XML来实现。

@Value:

  • 作用:用于注入基本类型和String类型的数据

  • 属性:
    value:用于指定数据的值。它可以使用spring中SpEL(也就是Spring的el表达式)
    SpEL的写法:${表达式}

3、用于改变作用范围
他们的作用就和在bean标签中使用scope属性实现的功能是一样的

@Scope:

  • 作用:用于指定bean的作用范围
  • 属性:
    value:指定范围的取值。常用取值:singleton prototype

4、和生命周期相关
他们的作用就和在bean标签中使用init-method和destroy-methode的作用是一样的

  • PreDestroy
    作用:用于指定销毁方法
  • PostConstruct
    作用:用于指定初始化方法

注解补充

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值