[英]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.