zoukankan      html  css  js  c++  java
  • 002649:spring的context:component-scan

    一、背景

      1、今天在设置项目时发现有些service和bo的实现类竟然在没有使用注解的情况下直接被使用@Autowired引用,且可正常使用   
      2、我惊呆了,根据以往的经验bean注入spring容器不是使用配置就是使用注解,今天眼前一亮。

    二、使用分析

      1、component-scan的功能

        

    <context:component-scan base-package="com.wjx.betalot" <!-- 扫描的基本包路径 -->
                            annotation-config="true" <!-- 是否激活属性注入注解 -->
                            name-generator="org.springframework.context.annotation.AnnotationBeanNameGenerator"  <!-- Bean的ID策略生成器 -->
                            resource-pattern="**/*.class" <!-- 对资源进行筛选的正则表达式,这边是个大的范畴,具体细分在include-filter与exclude-filter中进行 -->
                            scope-resolver="org.springframework.context.annotation.AnnotationScopeMetadataResolver" <!-- scope解析器 ,与scoped-proxy只能同时配置一个 -->
                            scoped-proxy="no" <!-- scope代理,与scope-resolver只能同时配置一个 -->
                            use-default-filters="false" <!-- 是否使用默认的过滤器,默认值true -->
                                      >
                <!-- 注意:若使用include-filter去定制扫描内容,要在use-default-filters="false"的情况下,不然会“失效”,被默认的过滤机制所覆盖 -->                   
                <!-- annotation是对注解进行扫描 -->
                <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/> 
                <!-- assignable是对类或接口进行扫描 -->
                <context:include-filter type="assignable" expression="com.wjx.betalot.performer.Performer"/>
                <context:include-filter type="assignable" expression="com.wjx.betalot.performer.impl.Sonnet"/>
                
                <!-- 注意:在use-default-filters="false"的情况下,exclude-filter是针对include-filter里的内容进行排除 -->
                <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
                <context:exclude-filter type="assignable" expression="com.wjx.betalot.performer.impl.RainPoem"/>
                <context:exclude-filter type="regex" expression=".service.*"/> 
           <!-- 两个点号表示子类 -->        <context:include-filter type="aspectj" expression="com.gongdao.etna..impl..*ServiceImpl"/> </context:component-scan>
        a、scan一般为需要注入spring中的类指明范围。     
        b、base-package是扫描的根目录,执行时会把这个目录下的所有class文件都加载到内存中。     
        c、use-default-filters 用于指明是否对上一步加载内存中的class筛选时使用默认过滤策略,包括component,service等注解信息,默认是true     
        d、include-filter 表示过滤式需要保留的文件,exclude-filter表示需要过滤掉的文件
        e、type 表示过滤的策略
        f、expression 表示过滤策略使用的表达式

      2、type类型

        

    三、源码分析

    四、结论

      1、这是一个需要了解源码实现的理由

    其他

      1、component注解注入的实例都为单例,其他注解可以配合@scope来指明实例的类型。
      2、@Controller,@Repository,@Service等注解在定义时都默认引入了@Component注解
  • 相关阅读:
    git如何将一个远程仓库的某个分支拉取到当前分支?
    linux下如何检查内核补丁是否符合社区代码规范?
    javascript快速入门20--Cookie
    javascript快速入门19--定位
    javascript快速入门18--样式
    javascript快速入门17--事件
    javascript快速入门16--表格
    javascript快速入门15--表单
    javascript快速入门15--节点
    javascript快速入门14--DOM基础
  • 原文地址:https://www.cnblogs.com/ws563573095/p/10311849.html
Copyright © 2011-2022 走看看