Announcement Announcement Module
Collapse
No announcement yet.
Conditional Logic around context element Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Conditional Logic around context element

    I have been looking around and have not found much on conditional logic I can place into the context xml that would optionally add load-time-weaver.

    For example, in WebSphere I want the line commented out

    <!-- uncomment the following line to run in Tomcat locally
    <context:load-time-weaver/>
    -->

    Yet, in Tomcat (i.e. our local development), I want the line active

    <context:load-time-weaver/>


    Is there a means to write something like

    <if condition=${tomcatServer}>
    <context:load-time-weaver/>
    </if>

    This is the ONLY line change in the entire config. Note: Please assume I would write something that would set the ${tomcatServer} variable



    Any suggestions?

  • #2
    I have solved a similar problem for deployment (dev or prod), I created two config files (appContextDS_dev.xml, appContextDS_prod.xml). I have my primary application context file (applicationContext.xml)

    In applicationContext.xml I import the (dev or prod) some thing like this

    <import resource="appContextDS_${server_env}.xml"/>

    Comment


    • #3
      Hey ptalakanti

      Thanks for the reply. I too have approached the the problem similar to you. I read the "env" via JNDI from the server directly. By doing so, I can build one ear for promotion with a checksum and signature and promote throughout the environments. The code required some extensions but essentially the web.xml looks like

      ...
      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext_env.xml</param-value>
      </context-param>
      <context-param>
      <param-name>contextConfigEnv</param-name>
      <param-value>ServerEnv</param-value>
      </context-param>
      <listener>
      <listener-class>com.xxx.util.spring.EnvContextLoaderListener </listener-class>
      </listener>
      ...
      <resource-ref>
      <description>The Server Environment Identifier</description>
      <res-ref-name>ServerEnv</res-ref-name>
      <res-type>java.lang.String</res-type>
      <res-auth>Container</res-auth>
      <res-sharing-scope>Shareable</res-sharing-scope>
      </resource-ref>

      Note: You cannot put EL (i.e. $env in the web.xml) so this just does a string substitution to become /WEB-INF/applicationContext_dev.xml, /WEB-INF/applicationContext_test.xml, etc.

      I take the same approach in non-web systems. However, see my next post about using profiles (new to Spring) to determine the configuration based on server target (i.e. Jetty or Tomcat).

      Kevin Clark

      Comment


      • #4
        Using profiles for conditional configurations

        My original problem was how to configure Spring to use the

        <context:load-time-weaver/>

        directive when using Tomcat and not to use it when running in say WebSphere or Jetty or WebLogic.

        Here is the best solution I found. Please comment

        1. Create two spring configurations.

        projectname-config-weaver.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <beans:beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns: p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:sec="http://www.springframework.org/schema/security"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schem...-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd"
        profile="tomcat">
        <context:load-time-weaver/>
        </beans:beans>

        projectname-config.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <beans:beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns: p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:sec="http://www.springframework.org/schema/security"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schem...-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schem...ontext-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
        default-autowire="byName">
        ...
        Note: NO PROFILE, so this is active on all servers

        In my case the web.xml has

        <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-security.xml, /WEB-INF/projectname-config.xml, /WEB-INF/projectname-config-weaver.xml</param-value>
        </context-param>

        In the tomcat VM args I add
        -Dspring.profiles.active=tomcat

        Comment

        Working...
        X