Spring注解——使用@ComponentScan自动扫描组件

文章系转载,方便整理和查看
作者:云原生实战
链接:https://www.jianshu.com/p/64aac6461d5b
来源:简书

1.创建一个配置类,在配置类上添加 @ComponentScan 注解。该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 context:component-scan。

package io.mieux.config;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan
public class BeanConfig {

}

2.使用 ApplicationContext 的 getBeanDefinitionNames() 方法获取已经注册到容器中的 bean 的名称。

import io.mieux.config.BeanConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App02 {
    public static void main(String[] args) {
        ApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(BeanConfig.class);

        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanName : beanDefinitionNames) {
            System.out.println("beanName: " + beanName);
        }
    }
}

运行效果:

beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalRequiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: beanConfig

除了 spring 本身注册的一些 bean 之外,可以看到最后一行,已经将 BeanConfig 这个类注册进容器中了。

3.指定要扫描的包(使用@ComponentScan 的 valule 属性来配置),创建一个controller 包,并在该包下新建一个 AppController 类。

package io.mieux.controller;

import org.springframework.stereotype.Controller;

@Controller
public class AppController {
}

在类上加了@Controller注解,说明该类是一个 Component。在 BeanConfig 类中修改:

package io.mieux.config;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan(value = "io.mieux.controller")
public class BeanConfig {

}

在 @ComponentScan 注解中指定了要扫描的包。

运行效果:

beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalRequiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: beanConfig
beanName: appController

AppController 已经被注册进容器了。

4.excludeFilters 和 includeFilters 的使用

使用 excludeFilters 来按照规则排除某些包的扫描。

package io.mieux.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

@ComponentScan(value = "io.mieux",
        excludeFilters = {@Filter(type = FilterType.ANNOTATION,
        value = {Controller.class})})
public class BeanConfig {

}

excludeFilters 的参数是一个 Filter[] 数组,然后指定 FilterType 的类型为 ANNOTATION,也就是通过注解来过滤,最后的 value 则是Controller 注解类。配置之后,在 spring 扫描的时候,就会跳过 io.mieux 包下,所有被 @Controller 注解标注的类。

使用 includeFilters 来按照规则只包含某些包的扫描。

在创建一个 service 的包,并创建一个 AppService 类,再加上一个 @Service 注解。

package io.mieux.service;

import org.springframework.stereotype.Service;

@Service
public class AppService {
}

修改 BeanCofig 类:

package io.mieux.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

@ComponentScan(value = "io.mieux", includeFilters = {@Filter(type = FilterType.ANNOTATION, classes = {Controller.class})})
public class BeanConfig {

}

运行效果:

beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalRequiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: beanConfig
beanName: appController
beanName: appService

配置里面,应该是只包含 @Controller 注解的类才会被注册到容器中,为什么 @Service 注解的类也被注册了呢?这里涉及到 @ComponentScan 的一个 useDefaultFilters 属性的用法,该属性默认值为 true,也就是说 spring 默认会自动发现被 @Component、@Repository、@Service 和 @Controller 标注的类,并注册进容器中。要达到只包含某些包的扫描效果,就必须将这个默认行为给禁用掉(在 @ComponentScan 中将 useDefaultFilters 设为 false 即可)。

package io.mieux.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

@ComponentScan(value = "io.mieux", 
        includeFilters = {@Filter(type = FilterType.ANNOTATION, classes = {Controller.class})},
        useDefaultFilters = false)
public class BeanConfig {

}

运行效果:

beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalRequiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: beanConfig
beanName: appController

5.添加多种扫描规则

1、如果使用的 jdk8,则可以直接添加多个 @ComponentScan 来添加多个扫描规则,但是在配置类中要加上 @Configuration 注解,否则无效。

package io.mieux.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan(value = "io.mieux.controller")
@ComponentScan(value = "io.mieux.service")
@Configuration
public class BeanConfig {

}

2、也可以使用 @ComponentScans 来添加多个 @ComponentScan,从而实现添加多个扫描规则。同样,也需要加上 @Configuration 注解,否则无效。

package io.mieux.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;

@ComponentScans(value = 
        {@ComponentScan(value = "io.mieux.controller"),
        @ComponentScan(value = "io.mieux.service")})
@Configuration
public class BeanConfig {

}

6.添加自定义过滤规则

在前面使用过 @Filter 注解,里面的 type 属性是一个 FilterType 的枚举类型:

public enum FilterType {

    ANNOTATION,
    ASSIGNABLE_TYPE,
    ASPECTJ,
    REGEX,
    CUSTOM

}

使用 CUSTOM 类型,就可以实现自定义过滤规则。

1、 首先创建一个实现 TypeFilter 接口的 CustomTypeFilter 类,并实现其 match 方法。

package io.mieux.config;

import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

public class CustomTypeFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader,
                         MetadataReaderFactory metadataReaderFactory) throws IOException {

        // 获取当前扫描到的类的注解元数据
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        // 获取当前扫描到的类的元数据
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        // 获取当前扫描到的类的资源信息
        Resource resource = metadataReader.getResource();

        if (classMetadata.getClassName().contains("Co")) {
            return true;
        }

        return false;
    }
}

这里简单对扫描到的类名进行判断,如果类名包含”Co“的就符合条件,也就会注入到容器中。

2、对 BeanConfig 进行修改,指定过滤类型为 Custom 类型,并指定 value 为 CustomTypeFilter.class。

package io.mieux.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

@ComponentScan(value = "io.mieux",
        includeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM, value = {CustomTypeFilter.class})},
        useDefaultFilters = false)
public class BeanConfig {

}

运行效果:

beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalRequiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: beanConfig
beanName: appController
### @ComponentScan 默认包可能引发的问题 当 `@ComponentScan` 被配置为扫描默认包(即未显式指定任何包路径)时,可能会遇到组件无法被正确加载的情况。这是因为 Spring 的类路径扫描机制依赖于明确的包结构来定位和注册组件。如果项目中的类文件位于不同的目录下而没有统一的根包名,则可能导致某些 Bean 定义丢失。 具体来说,在默认情况下,Spring Boot 应用程序会自动启用基于主应用程序类所在位置的组件扫描功能[^1]。然而,这种行为假设所有的业务逻辑都集中在一个共同的基础包之下;一旦违反此约定——比如将控制器、服务层和服务实现分布到多个独立命名空间里而不告知框架这些额外区域的存在——就容易造成遗漏重要部分的现象发生。 #### 解决方案一:明确指定基础包 通过设置属性 value 或 basePackages 来告诉容器应该查找哪些特定子包下的候选者们作为潜在管理对象: ```java @SpringBootApplication @ComponentScan(basePackages = {"com.example.project.controller", "com.example.project.service"}) public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 上述代码片段展示了如何手动定义两个主要关注领域的位置以便进行全面覆盖[^2]。 #### 解决方案二:调整项目布局 另一种方式则是重构整个工程架构使其遵循单一入口原则(Single Entry Point),即将所有相关联的功能模块统归置于某个固定的顶级父级节点之下。如此一来,默认的行为就能满足需求无需进一步干预。 另外值得注意的是,对于那些完全不需要纳入上下文中考虑范围内的辅助工具或者测试专用类别而言,可以利用 excludeFilters 参数排除它们从而优化启动效率并减少不必要的复杂度。 ```java @ComponentScan( basePackages = "com.example", excludeFilters = @Filter(type= FilterType.ANNOTATION, classes= Configuration.class)) ``` 这里演示了一个例子,其中我们决定忽略掉带有 @Configuration 注解标记的一切实例化请求[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值