如何仅在两个配置文件处于活动状态时才启动bean配置触发器

时间:2014-06-19 10:27:28

标签: spring spring-java-config spring-profiles

我有一个只有两个配置文件才能创建的bean" demo"和#34;本地"两者都很活跃。在基于Java的Spring配置中实现这一目标的最佳方法是什么。

到目前为止我想出的是,创建如下的bean:

@Profile("demo")
@Bean("isDemoActive")
public Boolean isDemoActive(){ return true;}

将那些注入bean创建方法并在这些bean上执行if条件。

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

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

我编写了一个@ProfileAll注释,它非常类似于标准@Profile注释,但注释的配置,方法,......只有在 ALL 的给定个人资料目前处于有效状态:

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。