如何从 Spring Boot 应用程序中排除特定的 @Configuration 类?

发布于 01-12 16:13 字数 2900 浏览 2 评论 0原文

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. @SpringBootApplication 注释中的 otherpackage.ComponentScanMarker.class 引用)

B) 完全排除 HateoasConfiguration*< /sup>.

我也尝试过:

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

尽管有 excludeFilters,但仍会加载 HateoasConfiguration

我尝试的另一个选项:

@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 中的 @Component 不加载。加载。我看过几个不同的类似问题和答案,但没有一个包含这两个目标的需要。

如何满足这两个需求,包括用于组件扫描的多个基础包,以及排除类路径上的特定 @Configuration 类?


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

@Configuration(proxyBeanMethods = false)
@EnablePluginRegistries({ LinkDiscoverer.class })
public class HateoasConfiguration {

Spring Boot 2.3.12 (I can't update to a newer version for reasons out of my control).

I have defined my main application class with specific scan base packages like this:

@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);
    }
}

What I'm trying to accomplish is both:

A) include a package outside the application's base package (hence the org.otherpackage.ComponentScanMarker.class reference in the @SpringBootApplication annotation)

and

B) exclude the HateoasConfiguration class completely*.

I've also tried this:

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

That results in HateoasConfiguration being loaded despite the excludeFilters.

Another option I tried:

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

That results in an exception at startup with the message:

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

I can't get it to work, no matter what combination of annotation properties I try. Either HateoasConfiguration gets loaded despite the attempt to exclude it, or @Components in org.otherpackage don't get loaded. I've looked at a few different similar questions and answers, but none of them include the need for both goals.

How can I accomplish both needs, to include multiple base packages for component scanning, and exclude a specific @Configuration class that's on the classpath?


* This question really has nothing to do with Spring HATEOAS, it's just an example of a @Configuration class that is on the classpath but I want Spring Boot to ignore. Here are the annotations present on that class (source code here):

@Configuration(proxyBeanMethods = false)
@EnablePluginRegistries({ LinkDiscoverer.class })
public class HateoasConfiguration {

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女净妖师2025-01-19 16:13:04

你试过这个吗?

@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);
    }
}

Have you tried this ?

@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);
    }
}
绅刃2025-01-19 16:13:04

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

根本问题是@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);
    }
}

I've been looking for a solution for a similar problem myself. Dropping it here for future developers.

The underlying problem is that @SpringBootApplication performs a full component scan itself, so your own @ComponentScan annotation does not have the desired effect.

The solution for me was to eject @SpringBootApplication and just do what it does internally (with our desired modifications) as it's just a convenience shorthand.

For your case, try:

@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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文