zoukankan      html  css  js  c++  java
  • 3、组件注册-@ComponentScan-自动扫描组件&指定扫描规则

    3、组件注册-@ComponentScan-自动扫描组件&指定扫描规则

    3.1 xml方式 benas.xml 导入context命名空间

    <?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.xsd">
    
        <!--包扫描 , 只要标注了 @Controller 、@Service、@Repository、@Component的类 都会被扫描-->
        <context:component-scan base-package="com.hw.springannotation"></context:component-scan>
    
        <bean id="pension" class="com.hw.springannotation.beans.Pension">
            <property name="name" value="hw"></property>
            <property name="age" value="18"></property>
        </bean>
    </beans>
    

    3.2 注解方式

    3.2.1 默认规则
    @ComponentScan(value = "com.hw.springannotation")
    
    3.2.2 指定排除扫描
    @ComponentScan(value = "com.hw.springannotation",
            excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})}
    )
    
    3.2.3 指定扫描(需要关闭默认配置)
    @ComponentScan(value = "com.hw.springannotation", useDefaultFilters = false,
            includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})}
    )       // 指定只扫描
    

    3.3 注意

    • 如果是jdk8 ,@Repeatable(ComponentScans.class) 重复组件,可以多写几次
    • 如果不是,可以使用 @ComponentScans 指定多个扫描组件
    @ComponentScans(value = {
            @ComponentScan(value = "com.hw.springannotation", useDefaultFilters = false,
                    includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})}
            ), @ComponentScan(value = "com.hw.springannotation",
            excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})})
    })
    
  • 相关阅读:
    前端框架-Bootstrap【搭建后台管理系统】
    前端-jQuery
    前端-js基础语法-DOM
    前端-js基础语法
    前端-html标签
    python学习并发编程
    python学习网络编程
    python爬虫学习:第一爬_快眼看书排行榜
    python总结:模块汇总
    python学习_解释器接口模块:sys
  • 原文地址:https://www.cnblogs.com/Grand-Jon/p/10018642.html
Copyright © 2011-2022 走看看