Using @Profile annotation with property place holder value

前端 未结 3 1783
挽巷
挽巷 2021-01-18 17:37

When we define profile for any component in spring, we declare it as @Profile(value=\"Prod\"). But i want to give that value from properties file. Is it possibl

3条回答
  •  遇见更好的自我
    2021-01-18 18:10

    Going through the source code of Spring, I have arrived to the conclusion that what you are asking is not possible. To make this clear, it is not possible to have Spring evaluate ${property} inside @Profile.

    Specifically take a look at ProfileCondition which checks whether or not the profile is active.

    class ProfileCondition implements Condition {
    
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            if (context.getEnvironment() != null) {
                MultiValueMap attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
                if (attrs != null) {
                    for (Object value : attrs.get("value")) {
                        if (context.getEnvironment().acceptsProfiles(((String[]) value))) {
                            return true;
                        }
                    }
                    return false;
                }
            }
            return true;
        }
    
    }
    

    The meat is context.getEnvironment().acceptsProfiles(((String[]) value)).

    Now if you check the source of AbstractEnvironment where acceptsProfiles resides, you will find that the control reaches

    protected boolean isProfileActive(String profile) {
        validateProfile(profile);
        return doGetActiveProfiles().contains(profile) ||
                (doGetActiveProfiles().isEmpty() && doGetDefaultProfiles().contains(profile));
    }
    

    which does not attempt to evaluate the expression, but takes the String verbatim (also note that nowhere before isProfileActive is the String expression being evaluated either)

    You can find the code I have mentioned above here and here.


    One another note, I am not sure why you would need to have a dynamic profile name.

提交回复
热议问题