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

    原文:http://jinnianshilongnian.iteye.com/blog/1762632

    component-scan的作用的自动扫描,把扫描到加了注解Java文件都注册成bean

    <context:component-scan base-package="com.target">
    </context:component-scan>

    今天在看一个代码项目时,看到有人使用了类似如下配置。看字面意思是要把@Service的注解包括进来,把@Controller的注解排除在外。

    然后那个代码分别在Springmvc的配置文件里面把@Service和@Repository排除,在Spring配置文件中把@Controller排除。

    至于为什么要排除,我起初以为性能问题,比如springmvc只需要关注controller,spring只需要关注service和repository。但是这是错误的认识,具体原因在最前面那个链接里有。

    同时include-filter也可以把base-packge包之外的其他包的给包括进来。

    include-filter如果放在exclude-filter后面则会报错。

    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>

    同时,context:component-scan还有一个默认属性use-default-filters="true",默认是true,即扫描Component(包括service、controller和repository)

    当只想包括某个注解时,需要显式关闭这个属性

    <context:component-scan base-package="com.target" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
  • 相关阅读:
    软件测试课堂练习
    JSP第一次作业
    安卓第六次作业
    安卓第五次作业
    第四次安卓作业
    JSP第四周
    软件测试课堂练习3.4
    Android数据库表
    Android购物菜单
    Android增删改查
  • 原文地址:https://www.cnblogs.com/kumu/p/7536680.html
Copyright © 2011-2022 走看看