Alireza Fattahi
Alireza Fattahi

Reputation: 45465

Spring more than one profile and ${spring.profiles.active}

In spring 4.1.2.RELEASE, we have 2 active profiles in web.xml

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>Production,Customer1</param-value>
</context-param>

And we want to dynamically load some property files as below:

<util:properties id="accountPolicy"
        location="classpath:/configs/${spring.profiles.active}/sample.properties" />

The ${spring.profiles.active} is not working, may be because there are two profiles, I tried some lookups like: ${spring.profiles.active[1]} but no luck !

Any comments

Updated:

It seems that ${spring.profiles.active} is an comma seperated list I try below:

<util:properties id="signConditions"
        location="classpath:/configs/#{ {'${spring.profiles.active}'.split(',')}.get(1) }/sample.properties" />

But the error seems that there will be an XML parsser error:

    org.springframework.expression.ParseException: 
Expression 'classpath:/configs/#{ {'Production,Customer1'.split('' @ 19: No ending suffix '}' for expression starting at character 19: #{ {'Production,Customer1'.split('

Upvotes: 1

Views: 3423

Answers (2)

Alireza Fattahi
Alireza Fattahi

Reputation: 45465

This did the job:

<util:properties id="signConditions"
        location="classpath:/configs/#{environment.getActiveProfiles()[1]}/sample.properties" />

Upvotes: 2

Adrian Shum
Adrian Shum

Reputation: 40036

I believe the more proper way is to do something like:

<beans profile="Production">
    <!-- some other stuff for Production profile -->
</beans>

<beans profile="Customer1">
    <util:properties id="accountPolicy"
        location="classpath:/configs/Customer1/sample.properties" />
    <!-- some other stuff for Customer1 profile -->
</bean>

Profiles are supposed to be used as Profiles of configurations in app context, instead of like a property for replacement (as what you are doing)


Edit base on my comment:

What you are looking for is not a proper use case of Spring profile feature (at least not now). What you are trying to do is having property place holder work base on a system property. However, activation of profiles can be done through other way. Which means, you can turn on a profile without that spring.profiles.active system property. What you are doing is not reliable.

If it is fine for you to pass in system properties, why not do something like:

  1. Have a profile called Customer, which denote for deployment to customers which will involve account policy (and other stuff)
  2. Pass in a system property, for example, with key = 'customerCode' and value being an identifier for a customer.

By doing so, what you need to do is

<beans profile="Production">
    <!-- some other stuff for Production profile -->
</beans>

<beans profile="Customer">
    <util:properties id="accountPolicy"
        location="classpath:/configs/${customerCode}/sample.properties" />
    <!-- some other stuff for Customer1 profile -->
</bean>

and system properties you need for your application should looks like: -Dspring.profiles.active="Production,Customer" -DcustomerCode=Customer1

Then you have proper use of profiles, and no need to duplicate accountPolicy for each customer.

Upvotes: 1

Related Questions