Spring 学习(六):Sprig Bean 自动注入

一、自动注入

在 spring 配置文件中对象名和 ref =“id” 名相同使用自动注入,可以不配置< constructor-arg>和< property> 元素,这有助于减少编写一个大的基于 Spring 的应用程序的 XML 配置的数量。

下列自动注入模式,它们可用于指示 Spring 容器为来使用自动装配进行依赖注入。你可以使用< bean>元素的 autowire 属性为一个 bean 定义指定自动注入模式。

  • no: 这是默认的设置,它意味着没有自动装配,你应该使用显式的bean引用来连线。
  • byName: 由属性名自动装配。Spring 容器看到在 XML 配置文件中 bean 的自动装配的属性设置为 byName。然后尝试匹配,并且将它的属性与在配置文件中被定义为相同名称的 beans 的属性进行连接。
  • byType: 由属性数据类型自动装配。Spring 容器看到在 XML 配置文件中 bean 的自动装配的属性设置为 byType。然后如果它的类型匹配配置文件中的一个确切的 bean 名称,它将尝试匹配和连接属性的类型。如果存在不止一个这样的 bean,则一个致命的异常将会被抛出。
  • constructor: 类似于 byType,但该类型适用于构造函数参数类型。如果在容器中没有一个构造函数参数类型的 bean,则一个致命错误将会发生。

二、Sprig 自动注入 byName

这种模式由属性名称指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName。然后,它尝试将它的属性与配置文件中定义为相同名称的 beans 进行匹配和连接。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

示例:

例如,在配置文件中有一个bean animal 定义设置为自动注入byName 并且它包含一个 cat 属性。那么 Spring 就会查找定义名为 cat 的 bean,并且用它来设置这个属性。你仍然可以使用 标签连接其余的属性。

applicationContext.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
             
        <bean id="cat" class="com.zhang.pojo.Cat"></bean>
      	<bean id="animal" class="com.zhang.pojo.Animal">
      		<property name="cat" ref="cat"></property>
      	</bean>
</beans>

Test 测试类 内容如下:

package com.zhang.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhang.pojo.Animal;

public class Test {

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		Animal animal = applicationContext.getBean("animal",Animal.class);
		
		System.out.println(animal);
	}
}
  • autowire 属性为 no ,将其配置改为:

      <bean id="cat" class="com.zhang.pojo.Cat"></bean>
      <bean id="animal" class="com.zhang.pojo.Animal" autowire="no"></bean>
    

    运行 Test测试类,其运行结果如下:
    在这里插入图片描述

  • autowire 属性为 byName ,将其配置改为:

    <bean id="cat" class="com.zhang.pojo.Cat"></bean>
    <bean id="animal" class="com.zhang.pojo.Animal" autowire="byName"></bean>
    

    运行 Test测试类,其运行结果如下:
    在这里插入图片描述

  • 若 配置文件中属性的类对象名与bean id 名不同,将其配置改为:

    <bean id="cat111" class="com.zhang.pojo.Cat"></bean>
    <bean id="animal" class="com.zhang.pojo.Animal" autowire="byName"></bean>
    

    运行 Test测试类,其运行结果如下:
    在这里插入图片描述

结果说明,只有在 autowire 属性为 byName,并且配置文件中属性的类对象名与bean id 名相同的情况下,Sping 才会自动注入。

三、Sprig 自动注入 byType

在上述基础上,将autowire 属性改为 byType

  • autowire 属性为 byName ,并且配置文件中属性的类对象名与bean id 名不同,将其配置改为:
     <bean id="cat111" class="com.zhang.pojo.Cat"></bean>
     <bean id="animal" class="com.zhang.pojo.Animal" autowire="byType"></bean>
    
    运行 Test测试类,其运行结果如下:
    在这里插入图片描述

结果说明,在 autowire 属性为 byType,并且配置文件中属性的类对象名与bean id 名不相同的情况下,Spring 仍然可以自动注入,因为 byType 是寻找配置文件中属性类的类型 与bean 的class属性相应时,即可自动注入。

但是,若存在多个类的不同 id bean会怎么样?

 <bean id="cat1" class="com.zhang.pojo.Cat"></bean>
 <bean id="cat2" class="com.zhang.pojo.Cat"></bean>
 <bean id="animal" class="com.zhang.pojo.Animal" autowire="byType"></bean>

运行 Test测试类,其运行结果如下:
在这里插入图片描述
它会报错,类型为 com.zhang.pojo.Cat 的 bean 没有定义:期望只找到一个 bean 但是 找了两个,说明 在 autowire 属性为 byType的情况下,不能定义多个属性类相同的bean标签。

四、设置默认方式

在实际开发中,会有多个bean配置,我们不能都为此配置 autowire 属性,所以我们可以配置一个默认属性,只需在配置文件 beans 定义中添加 default-autowire 属性即可。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"
        default-autowire="byName">
 </beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值