zoukankan      html  css  js  c++  java
  • Spring基础:自动配置bean,注解实现自动装配(重要)

    1,bean的自动装配

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

      在Spring中由3种装配方式

        在xml中显示配置

        在java中显示配置

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

    1.1 环境搭建测试

    实体类  

    public class Dog {
        public void shout(){
            System.out.println("wang~");
        }
    }
    
    
    public class Cat {
        public void shout(){
            System.out.println("miao~");
        }
    }
    
    
    public class People {
        private Cat cat;
        private Dog dog;
        private String name;
    }

    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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="cat" class="com.king.pojo.Cat"/>
        <bean id="dog" class="com.king.pojo.Dog"/>
    
        <bean id="people" class="com.king.pojo.People">
            <property name="name" value="king"/>
            <property name="cat" ref="cat"/>
            <property name="dog" ref="dog"/>
    
        </bean>
    
    
    </beans>

    单元测

        @Test
        public void test1(){
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            People people = context.getBean("people",People.class);
            people.getCat().shout();
            people.getDog().shout();
            System.out.println(people.getName());
        }

    1.2 自动装配(autowire)

        <bean id="cat" class="com.king.pojo.Cat"/>
        <bean id="dog11" class="com.king.pojo.Dog"/>
    
    
        <!--
          byName:会自动在容器上下文中查找,和自己对象set方法中形参对应的beanid!
             弊端:名字必须是能对应上的
          byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean!
             弊端:类型必须唯一不能重复
        -->
        <bean id="people" class="com.king.pojo.People" autowire="byType">
            <property name="name" value="king"/>
    
        </bean>

    小结:

      byname:需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法值一样

      byType:要保证所有bean的id的类型是唯一的,并且这个bean需要和自动注入的属性一致

    2,注解实现自动装配

      jdk1.5支持的注解,spring2.5开始支持注解

      The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML

      使用注解须知:

        1,导入约束,context约束(xmlns:context="http://www.springframework.org/schema/context"

        2,配置注解的支持

    <?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,直接在属性上使用即可!也可以在ser方法使用

    使用@Autowired可以省略掉set方法,(智能的,可用byType也可用byName)

    科普:

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

    也可以 

        //两种不同注解实现自动配置
        //此注解常用
        @Autowired
        @Qualifier(value = "cat2")
        private Cat cat;
    
        //byName的,也可以通过自定义名去找
        @Resource(name = "dog3")
        private Dog dog;
        private String name;

    小结:

      @Resource和@Autowired区别

        都是自动装配的注解,都可以放在属性字段上

        @Autowired 默认byType方式实现,如果没有在走byName,而且必须要求这个对象存在 【常用】

        @Resource默认通过byname的方式实现的,如果找不到名字,则按byType的方式实现,如果两个都找不到的情况下,就报错【常用】

        执行顺序不同

     

       

  • 相关阅读:
    【读书笔记】之《把时间当做朋友》
    软件工程——五大模型
    VB中的GetUserName函数
    VB中 vbp vbw frm frx log bas 等扩展名大全
    【机房收费系统】——基本数据设定的理解
    在64位WindowsServer2012R2中安装Oracle10g第二版(10.2.0.4.0)-20160106
    Linux——vi命令详解
    使用Postman测试WebService接口
    npm和yarn的淘宝镜像添加
    yarn配置私有registry
  • 原文地址:https://www.cnblogs.com/CL-King/p/13911210.html
Copyright © 2011-2022 走看看