SpringBoot 排除过滤器不适用于多个配置类



我正在开发一个依赖于另一个springboot应用程序的springboot应用程序。我想在父 springboot 应用程序中包含大多数 bean,但只有一个。

如何在不接触 ParentApplication 类的情况下排除父包已扫描的一个 springboot bean?

我尝试过但不起作用的方法:

1:在我的应用程序类中使用排除过滤来过滤掉特定的 Bean 类。

2:我还尝试排除 Bean 类和父配置类。

3:将 DisposableBean 接口添加到我要排除的 Bean 类中,并在运行时销毁它。

下面是我的应用程序初学者配置类和父类。

我的应用程序.class: 包 com.myapp;

@ComponentScan(
basePackages = {"com.parent",{my own packages..}},
excludeFilters= {
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value= {TheClassToExclude.class}),
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value= {ParentApplication.class})}   
)
@SpringBootApplication(exclude=ParentApplication.class)
public class MyApplication{
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@PostConstruct
public void init() {
System.out.println("App is initialized.");
}

}

我的家长申请.class

package com.parent;

@EnableRetry
@EnableScheduling
@SpringBootApplication(exclude = { HibernateJpaAutoConfiguration.class })
@ComponentScan(basePackages = {all the base package including the TheClassToExclude}
@PropertySource({all resources})
public class ParentApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@PostConstruct
public void haha() {
System.out.println("configuration class created");
}

控制台打印出:"已创建配置类",因此出于某种原因,ParentApplication 是由 springboot 启动的,因此我要排除的类也是如此。

仅供参考 - 我认为由于两个类都用@SpringBootApplication注释,因此都是@Configuration类,并且将参与自动 Spring 组件扫描 - 目前尚不清楚将首先扫描哪个类以"排除"另一个 - 除非......您显式指定入口点,因此,要加载的第一个 SpringBootApplication 类,如下所示

您可以通过在application.properties中设置logging.level.org.springframework=DEBUG来查看哪些类通过Spring组件扫描实例化以及以什么顺序实例化。

最新更新