Spring之IOC自动装配

导读:本篇文章讲解 Spring之IOC自动装配,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

7、Bean的自动装配

  • 自动装配是Spring满足bean依赖一种方式!
  • Spring会在上下文中自动寻找,并自动给bean装配属性!

在spring中有三种装配的方式

  • 在xml中显示的配置
  • 在java中显示配置
  • 隐式 的自动装配bean【重要】

7.1、测试

1.环境搭建:一个人有两个宠物!!

cat

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

dog

people

public class People {
    private Cat cat;
    private Dog dog;
    private String name;
}

beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

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

    <bean id="people" class="com.blue.pojo.People">
        <property name="name" value="blue"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>
</beans>
public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}

7.2、ByName自动装配

  • byName:根据属性名和id匹配,
  • byType:根据属性的类型和class匹配 全局唯一

在这里插入图片描述

7.3、ByType自动装配

byType:根据属性的类型和class匹配 全局唯一

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

    <!--
    自动装配:
    byName:会自动在容器的上下文中查找,和自己对象set方法后面的值对应的bean id
    byType:会自动在容器的上下文中查找,和自己对象属性相同的bean
    -->
    <bean id="people" class="com.automatic.pojo.People" autowire="byName">
        <property name="name" value="楠木" />
    </bean>

小结:

  • byname的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
  • byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!

7.4、使用注解实现自动装配

jdk1.5支持的注解,Spring2.5就支持注解了!
The introduction of annotation-based configuration raised the question of whether this approach is “better”than XML.(基于注释的配置的引入引发了这样一个问题:这种方法是比XML“更好”。)

要使用注解须知:

1.导入约束 context约束

2.配置注解的支持:context:annotation-config/

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
		<!--开启注解-->
    <context:annotation-config/>

</beans>

@Autowired

直接在属性上使用即可!也可以在set方式上使用!

使用Autowired我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字ByName!

科普:

@Nullable 字段标记了这个注解,说明这个字段可以为nu11;

源码:

public @interface Autowired {
    boolean required() default true;
}

@Autowired会先根据类型进行注入,如果容器中有多个满足类型的实例,就会根据ID进行注入。并不是单纯只根据类型注入

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
}

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解@Autowired完成的时候、我们可以使用@Qualifier(value = “XXX”)去配置@Autowired的使用,指定一个唯一的bean对象注入!

在这里插入图片描述

public class People {
    //如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
	@Autowired
	@Qualifier(value = "cat22")
	private Cat cat;
	@Autowired
	private Dog dog;
	private String name;
}

@Resource

也是需要唯一的状态。要么id唯一,要么数据类型唯一。

小结

@Resource和@ Autowired的区别

  • 都是用来自动装配的,都可以放在属性字段上
  • @Autowired 通过byType的方式实现,而且必须要求这个对象存在【常用】
  • @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错!【常用】
  • 执行顺序不同:Autowired 通过byType的方式实现;Resource默认通过byname的方式实现

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/120232.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!