【问题标题】:How can I exclude a specific @Configuration class from my Spring Boot application?如何从我的 Spring Boot 应用程序中排除特定的 @Configuration 类?
【发布时间】:2022-08-04 23:39:16
【问题描述】:

Spring Boot 2.3.12(由于我无法控制的原因,我无法更新到较新的版本)。

我已经用特定的扫描基础包定义了我的主要应用程序类,如下所示:

@SpringBootApplication(scanBasePackageClasses = {
                            MyApplication.class,
                            org.otherpackage.ComponentScanMarker.class
                            }
                        )
@ComponentScan(
            excludeFilters = {
                    @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
                    }
                )
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyApplication.class);
        application.run(args);
    }
}

我想要完成的是两个都

A)在应用程序的基本包之外包含一个包(因此org.otherpackage.ComponentScanMarker.class 引用在@SpringBootApplication 注释中)

B) 完全排除 HateoasConfiguration*.

我也试过这个:

@SpringBootApplication
@ComponentScan(
            basePackageClasses = {
                            MyApplication.class,
                            org.otherpackage.ComponentScanMarker.class
            },
            excludeFilters = {
                    @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
            }
                )

这导致HateoasConfiguration 被加载,尽管excludeFilters

我试过的另一个选择:

@SpringBootApplication(scanBasePackageClasses = {
                            MyApplication.class,
                            org.otherpackage.ComponentScanMarker.class
                        },
                        exclude = HateoasConfiguration.class
                        )

这会导致启动时出现异常并显示以下消息:

The following classes could not be excluded because they are not auto-configuration classes:
    - org.springframework.hateoas.config.HateoasConfiguration

无论我尝试哪种注释属性组合,我都无法让它工作。尽管尝试将其排除,HateoasConfiguration 仍会被加载,或者 org.otherpackage 中的 @Components 会被加载加载。我查看了几个不同的类似问题和答案,但没有一个包括对这两个目标的需求。

我怎样才能同时满足这两个需求,包括用于组件扫描的多个基础包,排除类路径上的特定@Configuration 类?


*这个问题确实与 Spring HATEOAS 无关,它只是类路径上的 @Configuration 类的一个示例,但我希望 Spring Boot 忽略。以下是该类上的注释(源代码here):

@Configuration(proxyBeanMethods = false)
@EnablePluginRegistries({ LinkDiscoverer.class })
public class HateoasConfiguration {
  • 请附上您尝试解决的特定问题。 * This question really has nothing to do with Spring HATEOAS, it\'s just an example of a third-party @Configuration Spring 有一些自动配置元素,例如 Hateoas。您身边的其他自定义组件应单独处理。请说清楚
  • @PanagiotisBougioukos 我不明白你的评论。该问题以多种方式说明了要完成的目标是什么。例如,\“我想要完成的是两者:...\”就在那里。
  • 请检查您的问题。你指定了一个特定的问题,然后你提到了* This question really has nothing to do with Spring HATEOAS, it\'s just an example of a third-party @Configuration class that is on the classpath but I want Spring Boot to ignore.。 Spring Hateoas 不是一个简单的 3rd 方库,而是一个 spring 库,开发人员可用的操作与一些自定义的外部 3rd 方库不同
  • 我从句子中删除了“第三方”。
  • 其他一些配置或自动配置是否可能依赖于这个类?在这种情况下,您还必须禁用它们。

标签: spring spring-boot component-scan


【解决方案1】:

你试过这个吗?

@SpringBootApplication(scanBasePackageClasses = {
                            MyApplication.class,
                            org.otherpackage.ComponentScanMarker.class
                            },
exclude = { HateoasConfiguration.class }
                        )
@ComponentScan(
            excludeFilters = {
                    @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
                    }
                )
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyApplication.class);
        application.run(args);
    }
}

【讨论】:

  • 我忘了提到这也是我尝试过的。我已使用该详细信息(以及由此产生的错误消息)更新了问题。
  • 你能显示HateoasConfiguration 的类定义吗?
  • 最后,我将其添加到问题中。
【解决方案2】:

我自己一直在寻找类似问题的解决方案。将其放在这里以供未来的开发人员使用。

根本问题是@SpringBootApplication 自己执行了全组件扫描,所以你自己的@ComponentScan 注解并没有达到预期的效果。

对我来说,解决方案是弹出@SpringBootApplication,然后只做它在内部做的事情(通过我们想要的修改),因为它只是一种方便的速记。

对于您的情况,请尝试:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
        basePackageClasses = {
                MyApplication.class,
                org.otherpackage.ComponentScanMarker.class
        },
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
                @ComponentScan.Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
        })
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyApplication.class);
        application.run(args);
    }
}

【讨论】:

    猜你喜欢
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2021-05-22
    • 2023-04-08
    • 2013-09-12
    相关资源
    最近更新 更多