1

I currently set my Spring profile using the below two files (Web.xml & MyListener.java). I set the "MyProfile" variable value as JNDI in Admin console and everything works fine.

But i was wondering whether there is a way to achieve this through applicationcontext.xml. I can read the JNDI value from Admin console as shown below in the applicationcontext.xml but not sure whether Spring has any way to set the profile in the xml file.

Thanks in advance!

Web.xml

    <context-param>
      <param-name>contextInitializerClasses</param-name>
      <param-value>MyListener</param-value>
    </context-param>

public class MyListener implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    public void initialize(ConfigurableApplicationContext applicationContext) {

        String actProfile = applicationContext.getEnvironment().getProperty("MyProfile");
        applicationContext.getEnvironment().setActiveProfiles(actProfile);
    }
}

applicationcontext.xml

<jee:jndi-lookup id="MyProfile" jndi-name="MyProfile"/>
Kgan
  • 35
  • 2
  • 12

1 Answers1

0

Not directly an answer to your question, but you can set the profile via an JVM property:

-Dspring.profiles.active=YOUR_PROFILE_NAMES_AS_COMMA_SEPARATED_LIST
Christof R
  • 863
  • 6
  • 10
  • Thanks Chris. Yes, i saw this option as well but if we could some how set it in applicationcontext.xml, it would be great. – Kgan Jul 23 '14 at 13:05
  • You might be lucky by using the `MethodInvokingFactoryBean` invoking `java.lang.System#setProperty()` with "spring.profiles.active" as key and `` as value. But I suppose it will be too late once this bean definition is evaluated. – Christof R Jul 23 '14 at 21:28
  • Yes, my guess is same.. it might be late. But i can try it out. – Kgan Jul 24 '14 at 21:12