I am trying to read properties from a properties file whose file name will be different for each of our environments, such as local.properties, dev.properties, etc. These property files will only contain connection information for the corresponding mongodb such as host, port and dbname. Typically, this kind of thing would be done with a JNDI definition on our application server, but there are currently no implementations for Mongo.
Since I use WebLogic 10.3.6, I cannot use the Servlet 3.0 specification and therefore cannot use the Java configuration for Spring, but only XML at this time. So the approach I'm trying to use is to have the contextInitializerClass context parameter defined in my web.xml and then set it for a class that implements ApplicationContextInitializer and sets the active Spring profile manually. However, when starting WebLogic or when redeploying, none of them call my custom initializer class, and my profile is not installed.
My question is , does Spring contextInitializerClass have a dependency on Servlet 3.0 or is there something else that I am missing?
The code I defined is:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextInitializerClass</param-name> <param-value>com.myapp.spring.SpringContextProfileInit</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </context-param> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> ...
SpringContextProfileInit.java
public class SpringContextProfileInit implements ApplicationContextInitializer<ConfigurableWebApplicationContext> { private static final Logger log = LoggerFactory.getLogger(SpringContextProfileInit.class); public SpringContextProfileInit() { log.debug("Got the constructor"); } @Override public void initialize(ConfigurableWebApplicationContext ctx) { ConfigurableWebEnvironment environ = ctx.getEnvironment(); log.debug("Got the environment, no profiles should be set: "+ environ.getActiveProfiles()); environ.setActiveProfiles("local"); log.debug("Now should be set to local: "+ environ.getActiveProfiles()); ctx.refresh(); } }
servlet-context.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> <context:property-placeholder properties-ref="deployProperties" /> ... <beans profile="local"> <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" p:location="WEB-INF/local.properties" /> </beans> <beans profile="beast, dev"> <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" p:location="WEB-INF/dev.properties" /> </beans> </beans>
When I try to deploy the application, I get an exception: NoSuchBeanDefinitionException:No bean named 'deployProperties' is defined what can be expected if the profile is not installed. My logs do not show that any of my debug statements are printed. I also tried moving the contextInitializerClass parameter to the init-param of my DispatcherServlet, but this gave the same results.
My limitations are that
I canβt install a profile from my Maven scripts because our company uses the same artifact to push all environments.
I also cannot change versions of WebLogic or use the latest spec servlet because it depends on the container.
My current versions are:
- Spring 3.1.2.RELEASE
- WebLogic 10.3.6
- javax.servlet-api 2.5
Has anyone else seen this problem and know how I can load the initializer class? Or is there a better way to do what I'm trying to do?
This is due to another poster issue that I didnβt answer: Spring MVC 3.1 Using profiles for environment-specific Hibernate settings