The answer from Thomasz is valid as long as the profile name can be provided statically in the web.xml file or a new type of configuration is used without XML code, where you can programmatically load the profile for installation from the properties file.
Since we are still using the XML version that I explored further, and found the following beautiful solution, in which you implement your own ApplicationContextInitializer , where you simply add a new PropertySource with a properties file to the list of sources to search for environment-specific configuration settings in In the example below, you can set the spring.profiles.active property in the spring.profiles.active file.
public class P13nApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { private static Logger LOG = LoggerFactory.getLogger(P13nApplicationContextInitializer.class); @Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); try { environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env.properties")); LOG.info("env.properties loaded"); } catch (IOException e) {
Then you need to add this initializer as a parameter to the ContextLoaderListener of spring, as shown below, in your web.xml :
<context-param> <param-name>contextInitializerClasses</param-name> <param-value>somepackage.P13nApplicationContextInitializer</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
You can also apply it to a DispatcherServlet :
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextInitializerClasses</param-name> <param-value>somepackage.P13nApplicationContextInitializer</param-value> </init-param> </servlet>
Fabian Dec 22 2018-11-11T00: 00Z
source share