自动装配Bean

前面我们聊了DI,即依赖注入,前面是通过在 beans.xml 以中一个一个标签去完成配置的,可在实际项目中,这种方式工作量就太大了,不实用啊。

那有没有省力快捷的方法呢?有,自动装配来了!

自动装配概念

自动装配是使用spring满足bean依赖的一种方法

spring会在应用上下文中为某个bean寻找其依赖的bean

实现自动装配

Spring的自动装配需要两个操作:

  1. 组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;

  2. 自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;

使用自动装配后,我们的显示的配置量明显降低。

自动装配方式

为了好理解先来三个类:

public class Cat {
   public void shout() {
       System.out.println("miao~");
  }
}

 

public class Dog {
   public void shout() {
       System.out.println("wang~");
  }
}

 

public class User {
   private Cat cat;
   private Dog dog;
   private String userName;
}

 

原来的注入方式:

   <bean id="dog" class="com.hwl.pojo.Dog"/>
   <bean id="cat" class="com.hwl.pojo.Cat"/>

   <bean id="user" class="com.hwl.pojo.User">
       <property name="cat" ref="cat"/>
       <property name="dog" ref="dog"/>
       <property name="userName" value="jake"/>
   </bean>

 

按名称自动装配 autowire byName

<bean id="user" class="com.hwl.pojo.User" autowire="byName">
   <property name="userName" value="jake"/>
</bean>

 

注意,需要正确的为需要注入的属性设置set方法,该方式依赖于set方法来完成。

过程:

  1. 查找其类中所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。

  2. 去spring容器中寻找是否有此字符串名称id的对象。

  3. 如果有,就取出注入;如果没有,就报空指针异常

按类型自动装配 autowire byType

需要保证:同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常:NoUniqueBeanDefinitionException

<bean id="user" class="com.hwl.pojo.User" autowire="byType">
   <property name="userName" value="jake"/>
</bean>

 

如果此时,在配置一个其他Cat的bean:

<bean id="cat" class="com.hwl.pojo.Cat"/>
<bean id="cat2" class="com.hwl.pojo.Cat"/>

 

这时候用 byType 就不行了。

使用注解

利用注解的方式注入属性,需要注意是 jdk1.5开始支持注解,spring2.5开始全面支持注解。

配置准备

1.在spring配置文件中引入context文件头

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

 

2.开启属性注解支持

<context:annotation-config/>

@Autowired

  • @Autowired是按类型自动转配的,不支持id匹配

  • 需要导入 spring-aop的包

测试一下:

将user类中的set方法都去掉,并改为使用@Autowired

public class User {
   @Autowired
   private Cat cat;
   @Autowired
   private Dog dog;
   private String userName;

   public Cat getCat() {
       return cat;
  }
   public Dog getDog() {
       return dog;
  }
   public String getUserName() {
       return userName;
  }
}

 

 

配置文件:

<context:annotation-config/>

<bean id="dog" class="com.hwl.pojo.Dog"/>
<bean id="cat" class="com.hwl.pojo.Cat"/>
<bean id="user" class="com.hwl.pojo.User"/>

 

我们发现这样已经注入成功了,已经不需要在id="user"的<bean>中去写了。

另外:

@Autowired(required=false)  说明:false,对象可以为null;true,对象必须存对象,不能为null

//如果允许对象为null,设置required = false,默认为true
@Autowired(required = false)
private Cat cat;

 

@Qualifier

  • @Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配

  • @Qualifier不能单独使用。

测试

<bean id="cat1" class="com.hwl.pojo.Cat"/>
<bean id="cat2" class="com.hwl.pojo.Cat"/>

没有加Qualifier测试,直接报错

在属性上添加Qualifier注解

@Autowired
@Qualifier(value = "cat2")
private Cat cat;
@Autowired
@Qualifier(value = "dog2")
private Dog dog;

成功输出!

 

@Resource

  • @Resource如有指定的name属性,先按该属性进行byName方式查找装配;

  • 其次再进行默认的byName方式进行装配;

  • 如果以上都不成功,则按byType的方式自动装配。

  • 都不成功,则报异常

public class User {
   //如果允许对象为null,设置required = false,默认为true
   @Resource(name = "cat2")
   private Cat cat;
   @Resource
   private Dog dog;
   private String userName;
}
<bean id="dog" class="com.hwl.pojo.Dog"/>
<bean id="cat1" class="com.hwl.pojo.Cat"/>
<bean id="cat2" class="com.hwl.pojo.Cat"/>

<bean id="user" class="com.hwl.pojo.User"/>

删掉cat2

@Resource
private Cat cat;
@Resource
private Dog dog;

OK,结论:先进行byName查找,失败;再进行byType查找,成功

@Autowired与@Resource异同

1、@Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。

2、@Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用

3、@Resource(属于J2EE复返),默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

它们的作用相同都是用注解方式注入对象,但执行顺序不同。@Autowired先byType,@Resource先byName。

 

posted @ 2021-10-17 15:13  四叶笔记  阅读(71)  评论(0编辑  收藏  举报