[英]How to Exclude Configuration from 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})
我知道这不起作用的原因是因为此消息不断弹出:
无法注册在 class 路径资源 [org/acme/webportal/service/jobmanager/logic/JConfiguration.class] 中定义的 bean 'mapUploadJobLauncher'。 已在 class 路径资源 [org/acme/webportal/service/jobmanager/logic/MockJConfiguration.class] 中定义了具有该名称的 bean,并且禁用了覆盖。
(这意味着我专门为测试定义的模拟配置与我真的想排除的生产配置冲突。)
我知道 Spring 引导对软件包来说真的很善变,但我完全不知道我在这里做错了什么。 如何从 IT 中排除配置?
所以在这种情况下的问题是附近的应用程序相互导入,但不是以有意义的方式。
还有第二个应用程序没有排除,因此 Spring 认为它可以添加排除的 class 反正。 我想要基于 Spring 测试应用程序的功能 / package,这似乎是不可能的。 我将它们合并到一个大应用程序中,现在排除工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.