如何从 Spring Boot 应用程序中排除特定的 @Configuration 类?
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 @Component
s 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 技术交流群。

发布评论
评论(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);
}
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
你试过这个吗?
Have you tried this ?