java—如何从spring it中排除配置?

iszxjhcz  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(248)

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

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})

我知道这不起作用的原因是,这个消息不断出现:
未能注册在类路径资源[org/acme/webportal/service/jobmanager/logic/jconfiguration.class]中定义的bean“mapuploadjoblauncher”。已在类路径资源[org/acme/webportal/service/jobmanager/logic/mockjconfiguration.class]中定义了具有该名称的bean,并且已禁用重写。
(这意味着我特别为测试定义的mock配置与我真正想排除的生产配置冲突。)
我知道SpringBoot在软件包方面真的很善变,但我完全不知道我在这里做错了什么。如何从it中排除配置?

k4aesqcs

k4aesqcs1#

因此,本例中的问题是,邻近的应用程序相互导入,但不是以有意义的方式导入。
还有第二个应用程序没有exclude,因此spring认为它可以添加excluded类。我想要一个基于特性/包的spring测试应用程序,这似乎是不可能的。我将它们合并到一个大的应用程序中,现在排除可以正常工作了。

相关问题