4

I am bit new on Spring Platform. I am implementing XML based Spring Profiles.

I have declared one profile in web.xml like

<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>

I want to get active profile value in my Java Class.

Here is my first attempt but it doesn't work due to some problems.

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
String profiles[] = ctx.getEnvironment().getActiveProfiles();
System.out.println(profiles[0]);

It displays me null value.Is there any idea how to activate profile in Java class?

jmj
  • 237,923
  • 42
  • 401
  • 438
user1030128
  • 411
  • 9
  • 23
  • 1
    you are reading wrong application context, if it is initialized by your web framework you probably need to retrieve that to get active profile, or else you can inject Environment bean where you want to refer it to and read it from there – jmj Jan 03 '13 at 22:36
  • thanks Jigar Joshi. Can you please guide me bit more? – user1030128 Jan 03 '13 at 22:40
  • Duplicate Post. Visit this link to see the answer for the question http://stackoverflow.com/questions/9267799/how-do-you-get-current-active-default-environment-profile-programatically-in-spr – Kris Jan 04 '13 at 03:24

2 Answers2

0

Create an ApplicationContextAware

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

  public class ApplicationContextProvider implements ApplicationContextAware {
       private static ApplicationContext applicationContext = null;

        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
              / / Assign the ApplicationContext into a static variable
             this.applicationContext = applicationContext;
        }
}

register it as bean in your spring xml file that your web.xml is configured to pick up

<bean id="myApplicationContextProvider" class="com.your.package.name.ApplicationContextProvider"></bean>

and from your java class now use

ApplicationContextProvider.getApplicationContext().getEnvironment().getActiveProfile()
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Thanks Jigar...I have done what u said but whenever i compile my Main Method. it throws me Exception in thread "main" java.lang.NullPointerException at com.inflinx.blog.springprofiles.web.controller.Test.main(Test.java:10) – user1030128 Jan 03 '13 at 23:17
  • String profiles[] = ApplicationContextProvider.getApplicationContext().getEnvironment().getActiveProfiles(); System.out.println(profiles.length); – user1030128 Jan 03 '13 at 23:19
0

You have a web.xml so that means you are deploying this to servlet container. Spring stores the ApplicationContext inside the ServletContext. With a ServletContext, you can get the ApplicationContext as follows...

ServletContext sc = request.getSession().getServletContext();
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);

If you want to set profiles inside a java application (main method) or a test, just set a system property for your profile...

java -Dspring.profiles.active="dev" org.hyness.MyClass

Your sample isn't working because you are instantiating a second context which doesn't use your web.xml.

hyness
  • 4,785
  • 1
  • 22
  • 24