zoukankan      html  css  js  c++  java
  • Spring学习笔记1---Bean的自动装配

    Bean的自动装配

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

    在Spring中有三种装配方式

    • 在xml中显示装配

    • 在java中显配置

    • 隐式的自动装配bean(重要)

    测试

    1.环境搭建

    • 一个人两条宠物

    ByName自动装配

    <!--
        byName: 会自动在容器上下文查找和自己Set方法后面的值相对性的beanid!
        -->
        <bean id="people" class="com.qi.pojo.Person" autowire="byName">
            <property name="name" value="qiqi"/>
    
         </bean>
    

    ByType自动装配

    <!--
    byType: 会自动在容器上下文查找和自己对象属性类型相同的bean! 必须保证这个类型全局唯一才可装配
    -->
    <bean id="people" class="com.qi.pojo.Person" autowire="byType">
        <property name="name" value="qiqi"/>
    
    </bean>
    

    小结:

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

    标题使用注解实现自动装配

    基于注释的配置的引入提出了一个问题,即这种方法是否比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
            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容器中存在,且符合名字byName

    小知识:

    @Nullable  字段表及了这个注释,说明这个字段可以为null
    

    在这里插入图片描述

    public @interface Autowired {
    
    	/**
    	 * Declares whether the annotated dependency is required.
    	 * <p>Defaults to {@code true}.
    	 */
    	boolean required() default true;
    
    }
    

    测试代码:

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

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

    public class Person {
        //如果显示定义了Autowired的required的属性为false,说明这个对象可以为空,否则不允许为空
        @Autowired
        @Qualifier(value = "cat22")
        private Cat cat;
        @Autowired
        @Qualifier(value = "dog22")
        private Dog dog;
        private String name;
    
    
    <bean id="cat2" class="com.qi.pojo.Cat"/>
        <bean id="dog2" class="com.qi.pojo.Dog"/>
        <bean id="cat22" class="com.qi.pojo.Cat"/>
        <bean id="dog22" class="com.qi.pojo.Dog"/>
    
        <bean id="people" class="com.qi.pojo.Person"/>
    

    @Resource注解

    <bean id="cat2" class="com.qi.pojo.Cat"/>
    <bean id="dog2" class="com.qi.pojo.Dog"/>
    <bean id="cat22" class="com.qi.pojo.Cat"/>
    <bean id="dog22" class="com.qi.pojo.Dog"/>
    
    <bean id="people" class="com.qi.pojo.Person"/>
    
    public class Person {
        @Resource(name = "cat22")
        private Cat cat;
        @Resource(name = "dog22")
        private Dog dog;
        private String name;
    
    

    小结:

    @Resource注解和@Autowired的区别:

    • 都是用来自动装配,都可以方式属性字段上

    • @Autowired通过byType的方式实现,而且必须要求这个对象存在(如果@Autowired不能自动装配,则需要通过 @Qualifier(value = “xxx”))

    • @Resource默认通过byName的方式实现,如果找不到byname,就通过bytype实现,如果两个都找不到的情况下,就报错

    • 执行顺序不同:@Autowired通过byType的方式实现,@Resource默认通过byName的方式实现

  • 相关阅读:
    《 基于UML技术的体育场馆管理系统设计与研究 》随笔
    《暗时间》读后感
    HTML 全局属性
    html简介
    Django中使用多线程发送邮件
    ubuntulinux 更改时区设置时间
    git 提交运用vim编辑器
    git配置
    网页显示403. That’s an error的解决方法。
    js 给url添加时间戳 解决浏览器缓存
  • 原文地址:https://www.cnblogs.com/zhahu/p/12333673.html
Copyright © 2011-2022 走看看