从弹簧启动应用程序中排除@Configuration

2022-09-01 23:26:19

我有一堆模块(比如3)。两个是基于弹簧启动的模块,另一个是基于弹簧的模块。说模块 1 - 弹簧靴模块 2 - 弹簧启动模块 3 - 通用模块仅基于弹簧

模块 3 @Configuration定义的文件,该文件仅需要由模块 2 而不是 1 选取。

我尝试了很多东西来排除配置文件。例如:-

@SpringBootApplication
@ComponentScan(basePackages = {"com.adobe"}
, excludeFilters = {
    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
public class Application {

  private static final Logger logger = LoggerFactory.getLogger(Application.class);

  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }

}

但是@Configiration类仍然没有被排除在外,Spring正试图在我不想要的应用程序上下文中加载它。我的配置类

@Configuration
public class WorkerConfig {

  @Bean
  public WorkerExecutors workerExec() {
    WorkerExecutors executors = new WorkerExecutors();
    return executors;
  }

}

我也确实在注释@ComponentScan读到

 * <p>Note that the {@code <context:component-scan>} element has an
 * {@code annotation-config} attribute; however, this annotation does not. This is because
 * in almost all cases when using {@code @ComponentScan}, default annotation config
 * processing (e.g. processing {@code @Autowired} and friends) is assumed. Furthermore,
 * when using {@link AnnotationConfigApplicationContext}, annotation config processors are
 * always registered, meaning that any attempt to disable them at the
 * {@code @ComponentScan} level would be ignored.

所以看起来排除在组件扫描不起作用。除此之外,我还尝试排除使用

@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})
public class Application {

但弹簧靴投掷

java.lang.IllegalStateException: The following classes could not be excluded because they are not auto-configuration classes:
    - com.adobe.repository.worker.lib.config.WorkerConfig
    - com.adobe.acp.repository.worker.lib.core.WorkerExecutors
    - com.adobe.acp.repository.worker.lib.core.Worker

任何想法,我如何禁用组件扫描弹簧启动模块中的非弹簧启动模块,而不是放入不同的包。我不想放入不同的包装。

任何帮助是值得赞赏的!


答案 1

你可以试试这个,它对我有用:

@SpringBootApplication
@ComponentScan(excludeFilters  = {@ComponentScan.Filter(
              type = FilterType.ASSIGNABLE_TYPE, classes = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})

答案 2

Auto-config packages 位于 org.springframework.boot.autoconfigure 下。这就是你不能这样做的原因:

@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})

春天做你吩咐做的事。您正在致电:

@ComponentScan(basePackages = {"com.adobe"}
, excludeFilters = {
    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})

因此,Spring 不会加载任何这些 Worker 类。这就是为什么Spring不会“执行”用@Configuration注释的类。

也就是说,你试图做的事情对我来说没有意义。听起来你有“模块”(java类),但它们都是同一个弹簧上下文的一部分。如果你有一个Spring Context,那么你可以告诉Spring加载一些@Configuration类,而不是其他一些。然后,从您的“模块”中,您可以注入所需的任何内容。模块 1 将从模块 3 中注入 Bean,但模块 2 不会。就是这么简单。

如果由于某种原因,您确实需要阻止模块 2 从模块 3 访问 Bean,但仍然保持模块 3 从模块 1 中可见,那么我会将模块 1 和模块 2 分隔在两个不同的 Spring Boot 应用程序中,模块 3 成为通用代码。但这种方法可能会破坏您当前的架构。

更新 周五 3月 29 2019

试试这个:

@SpringBootApplication
@ComponentScan(basePackages = { "com.myapp" }, excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { MyClass2.class }) })

它对我有用。我有MyClass和MyClass2,MyClass被加载,MyClass2没有。我尝试使用Spring Boot 1.5.9.RELEASE和Spring Bom来获取所有依赖项。


推荐