简体   繁体   English

排除基于应用程序 yaml 文件的自动配置 bean

[英]Exclude autoconfiguration beans based on application yaml file

I have some beans that want to exclude from auto configurating based on a configuration on my application.yml file:我有一些 bean 想要根据我的 application.yml 文件上的配置从自动配置中排除:

database:
  enabled: false

To do this, I created a Custom exclusion filter to exclude my beans from autoconfigurating:为此,我创建了一个自定义排除过滤器以将我的 bean 排除在自动配置之外:

public class DatabaseExclusionFilter implements AutoConfigurationImportFilter {

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


@Value("${database.enabled}")
private boolean enabled;

@Override
public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) {
    List<Boolean> excludeBeans = new ArrayList<>();
    if (!enabled) {
        excludeBeans = match(Arrays.asList(autoConfigurationClasses));
    }
    return convertToPrimitiveArray(excludeBeans);
}
}

This way, if the database is disabled in my project, I can exclude the configuration beans to being registered automatically.这样,如果数据库在我的项目中被禁用,我可以将配置 bean 排除在自动注册之外。 (This class is also indicated on the spring.factory file on the org.springframework.boot.autoconfigure.AutoConfigurationImportFilter). (这个class在org.springframework.boot.autoconfigure.AutoConfigurationImportFilter的spring.factory文件中也有注明)。

This works fine but I have one problem, I cannot read the values from my yaml file, I think it is a precedence problem, maybe when the AutoConfigurationImportFilter is called, the @Value is not ready to work yet.这工作正常,但我有一个问题,我无法从我的 yaml 文件中读取值,我认为这是一个优先级问题,可能在调用AutoConfigurationImportFilter时, @Value尚未准备好工作。 How can I solve this?我该如何解决这个问题?

Implement EnvironmentAware interface and get the property from the environment.实现EnvironmentAware接口并从环境中获取属性。

public class DatabaseExclusionFilter implements AutoConfigurationImportFilter, EnvironmentAware {

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

    private Environment environment;

    @Override
    public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) {
        List<Boolean> excludeBeans = new ArrayList<>();
        if (!Boolean.parseBoolean(environment.getProperty("database.enabled")))) {
            excludeBeans = match(Arrays.asList(autoConfigurationClasses));
        }
        return convertToPrimitiveArray(excludeBeans);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}

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

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