在Spring Boot中是否可以从另一个自动配置类中禁用一个自动配置类? [英] Is it possible to disable an autoconfiguration class from another autoconfiguration class in spring boot?

查看:159
本文介绍了在Spring Boot中是否可以从另一个自动配置类中禁用一个自动配置类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个数据访问库,并且希望能够以最小的配置将它作为依赖项包含在其他项目中(最好只是自动装配一个仓库).特别是,此库使用自动配置类(在spring.factories中启用)进行设置,并且需要禁用其他自动配置类才能工作(DataSourceAutoConfigurationHibernateJpaAutoConfiguration).

I'm working on a data access library and would like to be able to include it as a dependency in other projects with minimal configuration (ideally just autowire a repo). In particular, this library sets itself up using an autoconfiguration class (enabled in spring.factories) and needs to disable other autoconfiguration classes to work (DataSourceAutoConfiguration and HibernateJpaAutoConfiguration).

是否可以在依赖项目之外执行此操作?

Is it possible to do this outside of the dependent project?

为使配置尽可能简单,我想避免将排除项放在从属项目的@SpringBootApplication批注或其spring.autoconfigure.exclude属性中.

To make configuration as simple as possible I'd like to avoid putting excludes in the dependent project's @SpringBootApplication annotation or its spring.autoconfigure.exclude property.

更新:

在我的@Configuration上,我尝试添加注释:

On my @Configuration I have tried adding the annotations:

@EnableAutoConfiguration(exclude={
    DataSourceAutoConfiguration.class,
    HibernateJpaAutoConfiguration.class})

这会导致

IllegalStateException:配置问题:循环@Import具有 被检测到

IllegalStateException: Configuration problem: A circular @Import has been detected

@ImportAutoConfiguration(exclude={
    DataSourceAutoConfiguration.class, 
    HibernateJpaAutoConfiguration.class})

什么都不做.

推荐答案

有一个非常方便的接口,称为AutoConfigurationImportFilter,它决定应加载哪种自动配置.这也是@ConditionalOnClass注释的工作方式.

There's a very handy interface called AutoConfigurationImportFilter, which decides on which auto-configuration should be loaded. This is also how @ConditionalOnClass annotation works.

在您的库中,添加以下类:

In your library, add the following class:

public class MyExclusionFilter
        implements AutoConfigurationImportFilter {

    private static final Set<String> SHOULD_SKIP = new HashSet<>(
            Arrays.asList("org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration",
                    "org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration"));

    @Override
    public boolean[] match(String[] classNames, AutoConfigurationMetadata metadata) {
        boolean[] matches = new boolean[classNames.length];

        for(int i = 0; i< classNames.length; i++) {
            matches[i] = !SHOULD_SKIP.contains(classNames[i]);
        }
        return matches;
    }
}

该类需要在spring.factories中注册才能工作.将库中的以下行添加到META-INF/spring.factories中:

This class needs to be registered in spring.factories to work. Add the following line in the library into META-INF/spring.factories:

org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=com.mycompany.db.MyExclusionFilter

您不需要对从属项目进行任何更改.只需将库添加为依赖项,您指定的自动配置就不会加载到整个应用程序中.

You don't need to make any changes to the dependent project. Just add the library as a dependency and the auto-configuration you specified won't be loaded for the whole application.

注意::您可以添加多个导入过滤器,只有所有导入过滤器中未过滤的自动配置类会被加载.

NOTE: You can add more than one import filter, only the auto-configuration classes not filtered in all import filters will get loaded.

有关详细信息,请参见org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#filterorg.springframework.boot.autoconfigure.condition.OnClassCondition.java类的源代码.

For details, see the source code of org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#filter and org.springframework.boot.autoconfigure.condition.OnClassCondition.java classes.

这篇关于在Spring Boot中是否可以从另一个自动配置类中禁用一个自动配置类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆