从Spring Boot应用程序中排除@Configuration

时间:2019-03-28 17:31:17

标签: java spring spring-boot

我有一堆模块(比如说3)。两个是基于Spring Boot的模块,另一个是基于Spring的模块。 说模块1-SpringBoot     第2单元-Spring Boot     模块3-仅限通用模块基于Spring

定义的模块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

任何想法,除了放入不同的软件包之外,如何禁用Spring Boot模块中非Spring Boot模块的组件扫描。我不想放入其他包装。

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

您可以尝试此方法对我有用:

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

答案 1 :(得分:0)

自动配置软件包位于org.springframework.boot.autoconfigure下。这就是为什么你不能做的原因:

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

Spring完成了您要说的事情。您正在呼叫:

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

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

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

如果出于某种原因您确实需要阻止模块2从模块3访问bean,但仍然使模块3对模块1可见,那么我将在两个不同的Spring Boot应用程序中分别将模块1和模块2和模块3分开成为通用代码。但是这种方法可能会破坏您当前的体系结构。

2019年3月29日更新

尝试一下:

@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尝试了所有依赖项。

相关问题