如何使bean配置仅在两个概要文件处于活动状态时触发



我有一个bean,只有在两个配置文件"demo"one_answers"local"都处于活动状态时才应该创建它。在基于java的Spring配置中实现这一点的最佳方法是什么?

到目前为止,我想到的是创建如下bean:
@Profile("demo")
@Bean("isDemoActive")
public Boolean isDemoActive(){ return true;}

并将它们注入到bean创建方法中,并对这些bean执行if条件。

有更好/更简单的方法来做这种事情吗?

根据我上面的评论,我的建议是:

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class DoubleProfilesCondition implements Condition {
    public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata) {
        String[] activeProfiles = context.getEnvironment().getActiveProfiles();
        int counter = 0;
        for (int i = 0; i < activeProfiles.length; i++) {
            String profile = activeProfiles[i];
            if (profile.equals("profile1") || profile.equals("profile2")) {
                counter++;
            }
        }
        if (counter == 2)
            return true;
        return false;
   }
}

以及指定创建哪些bean的类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
@Conditional(DoubleProfilesCondition.class)
public class MyConfig {
    public @Bean
    ExampleService service() {
        ExampleService service = new ExampleService();
        service.setMessage("hello, success!");
        return service;
    }
}

我编写了一个@ProfileAll注释,它非常像标准的@Profile注释,但是注释了配置,方法,…仅在所有的给定配置文件当前处于活动状态时才由spring处理:

ProfileAll.java(注释)

import org.springframework.context.annotation.Conditional;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(ProfileAllCondition.class)
public @interface ProfileAll {
    /** The set of profiles for which the annotated component should be registered. */
    String[] value();
}

ProfileAllCondition.java(条件)

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;
class ProfileAllCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        if (context.getEnvironment() != null) {
            MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(ProfileAll.class.getName());
            if (attrs != null) {
                LinkedList<String> profilesActive = new LinkedList<>();
                LinkedList<String> profilesNotActive = new LinkedList<>();
                List<Object> values = attrs.get("value");
                int count = 0;
                for (Object value : values) {
                    for (String profile : ((String[]) value)) {
                        count++;
                        if (context.getEnvironment().acceptsProfiles(profile)) {
                            profilesActive.add(profile);
                        } else {
                            profilesNotActive.add(profile);
                        }
                    }
                }
                if (profilesActive.size() == count) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}

可以这样使用,例如:

@Profile({ "mysql", "cloud", "special" })
@Configuration
public class MySqlConfig {
...

MySqlConfig将只在所有三个配置文件当前都在启动时被处理。

相关内容

最新更新