Announcement Announcement Module
Collapse
No announcement yet.
Spring profiles not seen in root-context. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring profiles not seen in root-context.

    Hi All,

    MAVEN : 3
    Spring : 3.1.3.RELEASE

    I'm struggling for some time now with the integration of spring profiles in the root-context.xml.
    What I try todo :
    MAVEN runs profiles and creates a propertyfile with some properties in.
    One of those is spring.profiles.active wich contains the name of the profile that I want to see activated in Spring.

    In the root-context I create different profiles similar to the names in maven profiling, so that I can wire different beans depending on wich profile I run in MAVEN.
    The maven part is doing fine, depending on wich profile I run he sets spring.profiles.active to DEV/PRODUCTION/TEST/ACCEPTANCE wich is what i want.

    In the root-context try to I read this propertyfile and assumes that, cause spring.profiles.active is present, spring will switch to the correct profile.

    However, this is not the case.
    He always switches to the default profile, no matter what spring.profiles.active contains.
    I tried it with using a context-placeholder and a PropertyPlaceholderConfigurer defined in the root-context, however the result is the same.

    The test Bean I create that uses the placeholder spring.profiles.active does receive the correct profile name.

    Does anyone has an idea what is going wrong over here ?
    The property file is there and loaded fine (otherwise the placeholder wouldn't work for the dummybean )

    Hereunder a snippet of my files :
    root-context.xml

    Code:
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations" value="classpath:smalltracker.properties"/>
        </bean>
    
        <beans profile="dev">
            <bean class="be.brutje.smalltracker.business.DummyBean" id="dummyBean">
                <constructor-arg value="FOUND + ${spring.profiles.active}"/>
            </bean>
    	</beans>
        <beans profile="production">
            <bean class="be.brutje.smalltracker.business.DummyBean" id="dummyBean">
                <constructor-arg value="FOUND + ${spring.profiles.active}"/>
            </bean>
    	</beans>
        <beans profile="test">
            <bean class="be.brutje.smalltracker.business.DummyBean" id="dummyBean">
                <constructor-arg value="FOUND + ${spring.profiles.active}"/>
            </bean>
    	</beans>
        <beans profile="acceptance">
            <bean class="be.brutje.smalltracker.business.DummyBean" id="dummyBean">
                <constructor-arg value="FOUND + ${spring.profiles.active}"/>
            </bean>
    	</beans>
        <beans profile="default">
            <bean class="be.brutje.smalltracker.business.DummyBean" id="dummyBean">
                <constructor-arg value="DEFAULT + ${spring.profiles.active}"/>
            </bean>
    	</beans>
    </beans>
    web.xml
    Code:
    <?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">
    
        <!-- Creates the Spring Container shared by all Servlets and Filters -->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/root-context.xml
                /WEB-INF/spring/spring-security.xml
            </param-value>
        </context-param>
        <!-- Security -->
        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!-- Processes application requests -->
    	<servlet>
    		<servlet-name>appServlet</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>appServlet</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    
    </web-app>
    Code:
    smalltracker.properties
    smalltracker.version=${smalltracker.version}
    smalltracker.rootPath=${smalltracker.rootPath}
    smalltracker.environment=${smalltracker.environment}
    spring.profiles.active=${spring.profiles.active}
    It seems that he does not see the profile property in the root-context.
    Setting it with -Dspring.profiles.active works (but that is not what i want .

  • #2
    I've tried this using 2 web.xmls this works.

    pom.xml:

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <hibernate.version>4.0.1.Final</hibernate.version>
    <mysql.connector.version>5.1.18</mysql.connector.version>
    <slf4j.version>1.6.1</slf4j.version>
    <webXmlPath>${basedir}/src/main/resources/db/web.xml</webXmlPath>

    <spring.version>3.1.0.RELEASE</spring.version>


    </properties>


    <profiles>
    <profile>
    <id>mock</id>
    <properties>
    <webXmlPath>${basedir}/src/main/resources/mock/web.xml</webXmlPath>
    </properties>
    </profile>
    </profiles>

    and in the web.xml :

    <context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>mock,CGLib-Proxy</param-value>
    </context-param>

    In my case I have to load differnt things in spring: the entitymanager factory and an OpenEntityManagerInViewFilter for the database. Then this make sense, otherwise may be it is a little bit oversized ?

    Comment

    Working...
    X