How can I set active profile via annotation in spring?
You can pass in the active profile(s) at runtime using the spring.profiles.active
property:
-Dspring.profiles.active="profile1,profile2"
See the SpringSource blog post on the introduction of profiles.
If you are using making a standalone application or web application you pass active profile these way, according to Spring blog
Activation in Web application
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>spring.profiles.active</param-name> <param-value>production</param-value> </init-param></servlet>
Activation with manually created context
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();ctx.getEnvironment().setActiveProfiles("dev");ctx.load("classpath:/com/bank/config/xml/*-config.xml");ctx.refresh();