简体   繁体   English

如何从 Spring IT 中排除配置?

[英]How to Exclude Configuration from Spring IT?

It seems this is a very straightforward question, especially since it already works sometimes in my application.看起来这是一个非常简单的问题,特别是因为它有时已经在我的应用程序中工作了。 I have no idea why that is, so if I've missed some important information, just tell me.我不知道为什么会这样,所以如果我错过了一些重要信息,请告诉我。

First of all, I have a test case:首先,我有一个测试用例:

package org.acme.webportal.service;

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

}

This test has an application defined like this:这个测试有一个这样定义的应用程序:

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

This test excludes a configuration from the test which looks like this:此测试从测试中排除配置,如下所示:

package org.acme.webportal.security.ds;

@Configuration
public class DConfiguration {

}

Which, as far as I can tell, works perfectly.据我所知,这非常有效。 Now I want to exclude this configuration:现在我想排除这个配置:

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

@Configuration
public class JConfiguration {

}

How do I exclude this?我该如何排除这个?

I tried:我试过了:


// 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 reason I know this doesn't work is because this message keeps popping up:我知道这不起作用的原因是因为此消息不断弹出:

The bean 'mapUploadJobLauncher', defined in class path resource [org/acme/webportal/service/jobmanager/logic/JConfiguration.class], could not be registered.无法注册在 class 路径资源 [org/acme/webportal/service/jobmanager/logic/JConfiguration.class] 中定义的 bean 'mapUploadJobLauncher'。 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.已在 class 路径资源 [org/acme/webportal/service/jobmanager/logic/MockJConfiguration.class] 中定义了具有该名称的 bean,并且禁用了覆盖。

(Which means that the mock config I defined especially for the tests, is in conflict with the production config, which I really want to exclude.) (这意味着我专门为测试定义的模拟配置与我真的想排除的生产配置冲突。)

I know Spring Boot is really fickle with the packages, but I have no idea at all what I'm doing wrong here.我知道 Spring 引导对软件包来说真的很善变,但我完全不知道我在这里做错了什么。 How do I exclude the configuration from the IT?如何从 IT 中排除配置?

So the problem in this case is that Applications in close vicinity import each other, but not in the way it would make sense.所以在这种情况下的问题是附近的应用程序相互导入,但不是以有意义的方式。

There was a second application without the exclude, and so Spring figured it could just add the excluded class anyways.还有第二个应用程序没有排除,因此 Spring 认为它可以添加排除的 class 反正。 I wanted to have feature / package based Spring test application, this seems not to be possible.我想要基于 Spring 测试应用程序的功能 / package,这似乎是不可能的。 I merged them into one big application and now exclusion works normally.我将它们合并到一个大应用程序中,现在排除工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM