• Spring IOC容器基于注解方式装配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"
           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-4.2.xsd">
    
        <!--
            通过注解的方式配置bean,
            component-scan: 扫描包
            base-package:扫描该包下面的所有类,当扫描多个包时可以用逗号隔开
        -->
        <!--
            组件扫描:Spring能够自动从classpath下自动扫描、侦测和实例化具有特定注解的组件
            特定组件包括:
            1、 @Component   基本注解,将普通类标记为bean,加入到容器里,并且是单例模式
            2、 @Controller  标识表现层组件
            3、 @Service     标识服务层(业务层)组件
            4、 @Repository  标识持久层组件
    
            对于扫描到的组件,Spring有默认的命名策略,对于非限定类,第一个字母小写
            也可以在注解中通过value属性标识组件的名称
        -->
        <!--
            context:component-scan 元素会自动注册AutowiredAnnotationBeanPostProcessor(后置处理器)实例
            该实例可以自动装配带有@Autowired、@Resource 和@Inject注解的属性   注:@autowired 最常用
            一、@Autowired
                1、该注解会自动装配具有兼容类型的单个bean属性
                2、应用范围:构造器、普通字段(即使是非public),一切具有参数的方法都可以应用
                3、默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的bean装配属性时,会抛出异常
                   注:若某一属性允许不被设置,可以设置@Autowired的required属性为false
                4、默认情况下,当IOC容器里存在多个类型兼容的bean时,通过类型的自动装配无法正常工作,此时可以在@Qualifier
                   注解里提供Bean的名称,Spring允许对方法的入参标注@Qualifier以指定注入bean的名称
                   注:问题?如果使用@Autowired自动装配有多个的bean,想要匹配与之相应的bean,有几种解决方案?
                   1)与@Qualifier注解连用,指定相应bean的名称
                   2)在类上加@Repository、@Service时指定value属性,名字与匹配类相同,首字母小写
                5、将@Autowired标注在集合上
                   1)应用在数组类型上,Spring将会把所有匹配到的bean自动装配上
                   2)应用在集合属性上,Spring读取该集合的类型信息,然后自动装配与之兼容的bean
                   3)应用在Map上,如果map的健值为String,那么Spring会自动装配与Map值类型兼容的bean
                      此时bean的名称作为健值
            二、@Resource
               1、该注解与@Autowired注解类似,需要bean提供一个名称的属性,若该属性为空,
                  则自动采用标注处的变量或方法名作为Bean的名称
            三、@Inject
               1、该注解与@Autowired注解类似,只是没有required属性,一般能用@Autowired的就不用@Inject
                 Inject注解是javax JSR330规范之一,使用Inject注解需要添加Javax Inject依赖
        -->
        <!--
            @Autowired 与 @Resource注解的区别:
            1、@Autowired来源于Spring框架,@Resource来源于JSR-250规范
            2、@Autowired只按照byType注入,@Resource默认按byName注入
            3、@Autowired按类型装配依赖对象,默认情况下它要求对象必须存在,如果允许为null值,可以设置required属性为false。
               如果想要使用按名称装配,可以结合@Qualifier注解一起使用。
               @Resource中有两个重要的属性:type和name
               1)name属性指定byName,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称去寻找依赖对象
               2)如果按照名称也没有找到依赖对象时,@Resource注解会回退到按类型装配,如果指定了name属性,就只能按照名称装配了
             注:推荐使用@Resource注解在字段上,这样就不用些setter方法,而且这个注解属于J2EE的,降低的Spring的耦合性
                 代码看起来更加美观。
        -->
        <context:component-scan base-package="com.spring.cn.annotation"/>
    
    
        <!--
            resource-pattern: 只扫描特定的类而非基包下的所有类
            扫描controller包下的所有类
        -->
        <!--<context:component-scan base-package="com.spring.cn.annotation"-->
            <!--resource-pattern="controller/*.class"/>-->
    
        <!--
            1、exclude-filter:子节点,指定排除哪些指定表达式的组件
            2、include-filter:子节点,指定包含哪些指定表达式的组件,需要与use-default-filters="false"连用
            3、use-default-filters:默认为true
            4、在component-scan下可以拥有多个子节点
    
            exclude-filter和include-filter支持多种表达式,最常用的是annotation和assignable
            1、annotation   标注了注解的类  import org.springframework.stereotype.Controller;
            2、assignable   所有继承和扩展的类  package com.spring.cn.annotation.controller下面的类
        -->
        <!--<context:component-scan base-package="com.spring.cn.annotation" use-default-filters="false">-->
        <!--<context:component-scan base-package="com.spring.cn.annotation">-->
            <!--
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            -->
            <!--
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            -->
            <!--
           <context:exclude-filter type="assignable"
                                   expression="com.spring.cn.annotation.controller.UserController"/>
            -->
         <!--  <context:include-filter type="assignable"
                                   expression="com.spring.cn.annotation.controller.UserController"/>
       </context:component-scan>-->
    
    </beans>
    作者:donleo123
    本文如对您有帮助,还请多推荐下此文,如有错误欢迎指正,相互学习,共同进步。
  • 相关阅读:
    浅谈模块化开发
    用gulp搭建并发布自己的cli脚手架
    取值运算符新用法
    vue双向绑定之简易版
    获取对象属性之括号方式
    前端格式化工具之Prettier
    git操作之摘樱桃
    Sort
    MongoDB
    项目使用本地的包
  • 原文地址:https://www.cnblogs.com/donleo123/p/14069521.html
Copyright © 2020-2023  润新知