有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从Spring IT中排除配置?

这似乎是一个非常直截了当的问题,特别是因为它有时在我的应用程序中已经起作用了。我不知道为什么,所以如果我错过了一些重要信息,就告诉我

首先,我有一个测试用例:

package org.acme.webportal.service;

@SpringBootTest(classes = {TestApplication.class})
public class ServiceImplTest

}

此测试的应用程序定义如下:

package org.acme.webportal;

@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})
@PropertySource(value = {"classpath:application.properties"})
@EnableConfigurationProperties
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
    value = {DConfiguration.class})})
public class TestApplication extends SpringBootServletInitializer {

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

此测试从测试中排除如下配置:

package org.acme.webportal.security.ds;

@Configuration
public class DConfiguration {

}

据我所知,这种方法非常有效。现在我想排除此配置:

package org.acme.webportal.service.jobmanager.logic;

@Configuration
public class JConfiguration {

}

我如何排除这个

我试过:


// just as the working exclusion above-> does not work
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {JConfiguration.class})})

// add it to the working excludeFilter -> does not work for JConfiguration, only for DConfiguration
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {DConfiguration.class, JConfiguration.class})})

// add it to the working excludeFilters -> does not work for JConfiguration, only for DConfiguration
@ComponentScan(excludeFilters = {
    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {DConfiguration.class}),
    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {JConfiguration.class})
})

// maybe as a pattern? the pattern might be wrong, the config is still present
@ComponentScan.Filter(type = FilterType.REGEX, pattern = {"org\\.acme\\.webportal\\.service\\.jobmanager\\.logic.*"})

// does not work because it's no auto configuration
@SpringBootApplication(exclude = {JConfiguration.class})

// does not work because it's no auto configuration
@EnableAutoConfiguration(exclude = {JConfiguration.class})

我知道这不起作用的原因是,这条消息不断弹出:

The bean 'mapUploadJobLauncher', defined in class path resource [org/acme/webportal/service/jobmanager/logic/JConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/acme/webportal/service/jobmanager/logic/MockJConfiguration.class] and overriding is disabled.

(这意味着我专门为测试定义的模拟配置与我真正想要排除的生产配置冲突。)

我知道SpringBoot的软件包很易变,但我完全不知道我在这里做错了什么。如何从IT中排除配置


共 (1) 个答案

  1. # 1 楼答案

    因此,本例中的问题是,邻近的应用程序相互导入,但不是以有意义的方式导入

    还有第二个应用程序没有exclude,因此Spring认为它可以添加excluded类。我想要一个基于功能/包的Spring测试应用程序,这似乎不可能。我将它们合并到一个大应用程序中,现在排除功能正常工作