@ComponentScan注解中排除@Service注解不生效问题

问题

在应用开发过程中,需要排除某些注解下的bean,但是排除配置未生效!

package com.fjd.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

/**
 * 使用场景说明
 * 这种配置表示:
 * 扫描 com.fjd 包
 * 排除所有被 @Service 注解的类
 * 包含被 @Repository 注解的类(需单独导入)
 * 使用 AspectJ 模式排除 com.fjd.dao 包及其子包
 */

@Configuration
@ComponentScan(
        value = "com.fjd",
        //includeFilters = @Filter(type = FilterType.ANNOTATION, classes = Repository.class),
        excludeFilters = {@ComponentScan.Filter
                (type = FilterType.ANNOTATION,
                 classes = Service.class) // 排除 @Service
              // @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "com.fjd.dao..*") // 排除 DAO 包
        }
)
public class SpringConfig {
    // 其他配置...
}

仍能正常运行。
在这里插入图片描述

原因分析

  • 组件扫描范围问题‌
    确保被@Service注解的类确实位于com.fjd包或其子包下
    检查是否有其他@ComponentScan配置覆盖了当前配置
  • 依赖问题‌
    确认项目中正确引入了spring-context依赖,版本与Spring Boot兼容
    检查是否存在依赖冲突导致注解失效

问题解决

存在其他@ComponentScan配置覆盖了当前配置
在这里插入图片描述
注释其他配置扫描,配置生效!在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值