实战 在Spring IOC容器中 实现 Bean 的自动装配和 注解实现 自动装配方式 (五)

一、基于Bean 的自动装配方式
  • 自动装配是Spring满足Bean依赖的一种方式!
  • Spring会在上下文中自动寻找,并自动给Bean装配属性!
在Spring中有三种装配的方式

1、在xml 中显示配置
2、在Java中显示配置
3、隐式的自动装配Bean【重要】

  • byName:会自动在容器上下文中查询,和自己对象set 方法后面的值对应beanid。

<bean id="cat" class="com.ZQQQ.pojo.Cat"/>
<bean id="dog" class="com.ZQQQ.pojo.Dog"/>
	<!--byName 的方式自动装配 -->
<bean name="person" class="com.ZQQQ.pojo.Person" autowire="byName"/>
</beans>
  • byType:会自动在容器上下文中查找,和自己对象属性类型形同的Bean。

<?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="cat" class="com.ZQQQ.pojo.Cat"/>
<bean id="dog" class="com.ZQQQ.pojo.Dog"/>
<!--byName 的方式自动装配 -->
<bean name="person" class="com.ZQQQ.pojo.Person" autowire="byType"/>
</beans>
  • 小结:
  • byName的时候,需要保证所有Bean的id唯一,并且这个Bean需要和自动注入的属性set方法的值一致!
  • byType 的时候,需要保证所有Bean的class唯一,并且这个Bean需要和自动注入的属性类型一致!
二、基于注解实现自动装配方式
  • 概述:JDK1.5 就支持注解了。Spring 2.5 就支持注解了
  • 使用注解自动装配需要导入的依赖如下:

<?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: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">

<context:annotation-config/>
</beans>
完整版案例:
  • pojo类:

public class Cat {
public void shot(){
System.out.println(“喵喵~”);
}
}

public class Dog {
public void shot(){
System.out.println(“汪汪~~”);
}
}

package com.ZQQQ.pojo;
import org.springframework.beans.factory.annotation.Autowired;
/*
* 自动装配Bean 学习
* 在上下文配置中dog、cat 是引用类型,所以使用ref
*/

public class Person {
@Autowired
private Dog dog;
@Autowired
private Cat cat;
private String name;
/**
* 使用@Autowired 实现自动装配Bean 不需要提供set 方法,只需get方法即可!!
* /
public Dog getDog() {
	return dog;
}
public Cat getCat() {
	return cat;
}
public String getName() {
	return name;
}

@Override
public String toString() {
	return "Person{" +
			"dog=" + dog +
			", cat=" + cat +
			", name='" + name + '\'' +
			'}';
}
}
  • 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: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">
<bean id="cat" class="com.ZQQQ.pojo.Cat"/>
<bean id="dog" class="com.ZQQQ.pojo.Dog"/>
<!--byName 的方式自动装配 -->
<bean name="person" class="com.ZQQQ.pojo.Person" autowire="byName"/>
<!--开启基于注解的自动装配的方式 -->
<context:annotation-config/>
</beans>
  • 测试用例

import com.ZQQQ.pojo.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void Person(){
ApplicationContext context = new ClassPathXmlApplicationContext(“Applicationcontext.xml”);
// 如下注意类型要和上下文中的类型名称保持一致
Person bean = context.getBean(“person”, Person.class);
bean.getCat().shot();
bean.getDog().shot();
}
}

打印结果:

在这里插入图片描述

@Autowired 使用
  • @Autowired 直接在属性上使用即可,也可以在set 方法上使用。

  • 使用@Autowired 可以省略Set方法,前提是这个自动装配的的属性在IOC(Spring容器)中存在,且符合命名字:ByName!

@Nullable使用
  • @Nullable
    字段标记了这个注解,说明这个字段可以为null
@Autowired与@Qualifier(value = “xxxx”) 组合使用
  • @Autowired
    @Qualifier(value = “dog222”)
    Autowired和Qualifier 二者组合使用可以显示的指定唯一的id,实现自动装配。
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在使用的时候,在各自对应的属性上加上注解不然会报错。谨记!!!
@Resource 使用

1.使用方法:在使用的时候也是在属性上面加上此注解也可以实现自动装配Bean的功能!
2.@Resource(name = “Xxxxx”) 也可以指定唯一id,实现自动装配。但需要在resources下的Bean容器中的Id 和你在属性类中的名称保持一致。

小结:
  • @Autowired和 @Resource 区别:

1.都可以用来实现自动装配Bean 的功能,都可以放在属性字段上。
2.@Autowired 通过bytype 的方式实现,而且必须要求这个对象存在。
3.@Resource 默认通过byname 的方式实现,如果找不到名字,则通过byType 的方式实现!如果两个都找不到的情况下就报错!

执行顺序:
@Autowired通过bytype的方式实现
@Resource 默认通过byName的方式实现

使用基于注解实现自动装配需要导入对应的约束。

在这里插入图片描述

-----------------------------------------要克服生活的焦虑和沮丧,得先学会做自己的主人-----------------------------------------

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值