![](/img/trans.png)
[英]Is it possible to exclude nested configuration from autoconfiguration in Spring Boot application?
[英]Exclude @Configuration from Spring Boot Application
我有一堆模块(比如说3)。 两个是基于Spring Boot的模块,另一个是基于Spring的模块。 说出模块1-SpringBoot模块2-Spring Boot模块3-仅通用模块基于Spring
定义了模块3 @Configuration文件,该文件仅需要模块2而不是1来选择。
我尝试了一堆东西来排除配置文件。 例如:
@SpringBootApplication
@ComponentScan(basePackages = {"com.adobe"}
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
但是,仍然没有将@Configiration类排除在外,Spring尝试在我不希望的应用程序上下文中加载它。 我的配置类
@Configuration
public class WorkerConfig {
@Bean
public WorkerExecutors workerExec() {
WorkerExecutors executors = new WorkerExecutors();
return executors;
}
}
我也读过@ComponentScan批注
* <p>Note that the {@code <context:component-scan>} element has an
* {@code annotation-config} attribute; however, this annotation does not. This is because
* in almost all cases when using {@code @ComponentScan}, default annotation config
* processing (e.g. processing {@code @Autowired} and friends) is assumed. Furthermore,
* when using {@link AnnotationConfigApplicationContext}, annotation config processors are
* always registered, meaning that any attempt to disable them at the
* {@code @ComponentScan} level would be ignored.
因此,看起来像在组件扫描中排除将不起作用。 除了上述以外,我还尝试排除使用
@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})
public class Application {
但是春季靴子抛出
java.lang.IllegalStateException: The following classes could not be excluded because they are not auto-configuration classes:
- com.adobe.repository.worker.lib.config.WorkerConfig
- com.adobe.acp.repository.worker.lib.core.WorkerExecutors
- com.adobe.acp.repository.worker.lib.core.Worker
任何想法,除了放入不同的软件包之外,如何禁用在Spring Boot模块中的非Spring Boot模块的组件扫描。 我不想放入其他包装。
任何帮助表示赞赏!
您可以尝试此方法对我有用:
@SpringBootApplication
@ComponentScan(excludeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE, classes = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
自动配置软件包位于org.springframework.boot.autoconfigure下。 这就是为什么你不能做的原因:
@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})
春天照你说的去做。 您正在致电:
@ComponentScan(basePackages = {"com.adobe"}
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
因此,Spring将不会加载任何这些Worker类。 这就是为什么Spring不会“执行”用@Configuration注释的类的原因。
就是说,您想做的事对我来说毫无意义。 听起来好像有“模块”(java类),但是它们全部都属于同一spring上下文。 如果您只有一个Spring Context,则可以告诉Spring加载一些@Configuration类,而不是其他一些。 然后,您可以从“模块”中注入所需的任何内容。 模块1将注入来自模块3的Bean,但模块2将不会注入。 就那么简单。
如果由于某种原因您确实需要阻止模块2访问模块3中的bean,但仍然使模块3在模块1中可见,那么我将在两个不同的Spring Boot Apps中将模块1和模块2分开,并且模块3成为通用代码。 但是这种方法可能会破坏您当前的体系结构。
2019年3月29日星期五更新
尝试这个:
@SpringBootApplication
@ComponentScan(basePackages = { "com.myapp" }, excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { MyClass2.class }) })
它为我工作。 我有MyClass和MyClass2,而MyClass已加载,而MyClass2没有。 我使用Spring Boot 1.5.9.RELEASE和Spring Bom尝试了所有依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.