@ComponentScan-自动扫描组件(指定扫描规则)

@ComponentScan-自动扫描组件(指定扫描规则)

最早的时候使用Spring框架的时候我们开启组件扫描都是通过在spring的xml配置文件中通过component-scan标签来配置自动扫描组件

开启自动扫描组件的作用:(对于这一点其实很多人是不知道的)

开启自动扫描组件之后, 对于扫描的包中的被:

  1. @Controller
  2. @Service
  3. @Repository
  4. @Component

注解修饰的类都会被添加到容器中

现在我们使用注解方式来完成自动扫描组件:

1. 创建Controller和Service和Dao层类用于测试

package com.ffyc.spring.controller;

import org.springframework.stereotype.Controller;

@Controller
public class TestContoller {
}
package com.ffyc.spring.service;

import org.springframework.stereotype.Service;

@Service
public class TestService {
}
package com.ffyc.spring.dao;

import org.springframework.stereotype.Repository;

@Repository
public class TestDao {
}

2.创建一个配置类, 在配置类上添加@ComponentScan注解开启组件扫描

package com.ffyc.spring.config;

import com.ffyc.spring.model.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.ffyc.spring")
public class PersonConfig {

    @Bean
    public Person getPerson() {
        return new Person();
    }
}

3.创建一个测试类, 进行测试:

package com.ffyc.spring.test;

import com.ffyc.spring.config.PersonConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test2 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
        String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
        for(String name : beanDefinitionNames) {
            System.out.println(name);
        }
    }
}

通过测试结果我们可以发现此时IOC容器中是由对应的这几个Controller和Service层和dao层的类的, 也就是说自动扫描是成功的

4. 扩展: 指定扫描规则 --> @Controller注解修饰的不扫描

package com.ffyc.spring.config;

import com.ffyc.spring.model.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScan(value = "com.ffyc.spring" , excludeFilters = {
        @ComponentScan.Filter(classes = Controller.class)
})
public class PersonConfig {

    @Bean
    public Person getPerson() {
        return new Person();
    }
}

主要看以下这段代码:

@ComponentScan(value = "com.ffyc.spring" , excludeFilters = {
        @ComponentScan.Filter(classes = Controller.class)
})

以前我们的初学spring的时候使用spring的xml配置文件的方式配置过滤规则的时候其实也是使用的ComponentScan标签的excludeFilters属性来完成的

至于上面为什么excludeFilters属性值是一个注解数组, 这个其实看底层源码就能看到:

给出源码:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    ...
    Filter[] includeFilters() default {};

	/**
	 * Specifies which types are not eligible for component scanning.
	 * @see #resourcePattern
	 */
	Filter[] excludeFilters() default {};
    ...
}

我们点入到Filter类中, 可以发现Filter是一个内部注解类, 是@ComponentScan类的一个内部注解类:

@Retention(RetentionPolicy.RUNTIME)
@Target({})
@interface Filter {
    FilterType type() default FilterType.ANNOTATION;
    @AliasFor("classes")
    Class<?>[] value() default {};
    @AliasFor("value")
    Class<?>[] classes() default {};
    String[] pattern() default {};

}

5. 扩展 : 指定扫描规则 --> 只扫描@Controller注解修饰的

package com.ffyc.spring.config;

import com.ffyc.spring.model.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;

@Configuration
//@ComponentScan(value = "com.ffyc.spring" , excludeFilters = {
//        @ComponentScan.Filter(classes = Controller.class)
//})
@ComponentScan(value = "com.ffyc.spring", includeFilters = {
        @ComponentScan.Filter(classes = Controller.class)
}, useDefaultFilters = false)
public class PersonConfig {

    @Bean
    public Person getPerson() {
        return new Person();
    }
}

主要代码如下:

@ComponentScan(value = "com.ffyc.spring", includeFilters = {
        @ComponentScan.Filter(classes = Controller.class)
}, useDefaultFilters = false)

可以看到, 我们如果要指定扫描规则为只扫描某些的时候, 这个时候我们还要将@ComponentScan的useDefaultFilters属性赋值为false, 这个属性默认是true

  • 给出userDefaultFilters属性源码:

    /**
    	 * Indicates whether automatic detection of classes annotated with {@code @Component}
    	 * {@code @Repository}, {@code @Service}, or {@code @Controller} should be enabled.
    	 */
    	boolean useDefaultFilters() default true;
    
    
    • 可以看到默认值确实是true
  • 这个属性就是开启默认包扫描规则, 这个默认规则是全部扫描, 如果你不将这个默认包扫描规则关闭掉, 就不能通过includeFilters注解来只扫描某些类

其实如果有小伙伴记得, 我们其实在初期的学习Spring的时候通过xml配置文件方式设置包扫描规则, 如果是配置只扫描某些类的时候也是通过ComponentScan注解中的useDefaultFilters属性, 将该属性设置为了false

  • 其实无论如何都要知道, 注解方式其实底层和xml方式都是一样的, 只不过注解方式简化了xml方式而已
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值