Back Forum Reply New

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

lt;!-- uncomment the following line to run in Tomcat locally
lt;context:load-time-weaver/gt;--gt;

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

lt;context:load-time-weaver/gt;Is there a means to write something like

lt;if condition=${tomcatServer}gt;
lt;context:load-time-weaver/gt;
lt;/ifgt;

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?

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

lt;import resource=quot;appContextDS_${server_env}.xmlquot;/gt;

Hey ptalakanti

Thanks for the reply. I too have approached the the problem similar to you. I read the quot;envquot; 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
...
lt;context-paramgt;lt;param-namegt;contextConfigLocationlt;/param-namegt;
lt;param-valuegt;/WEB-INF/applicationContext_env.xmllt;/param-valuegt;
lt;/context-paramgt;
lt;context-paramgt;lt;param-namegt;contextConfigEnvlt;/param-namegt;
lt;param-valuegt;ServerEnvlt;/param-valuegt;
lt;/context-paramgt;
lt;listenergt;lt;listener-classgt;com.xxx.util.spring.EnvContextLoaderListener  lt;/listener-classgt;
lt;/listenergt;
...
lt;resource-refgt;lt;descriptiongt;The Server Environment Identifierlt;/descriptiongt;
lt;res-ref-namegt;ServerEnvlt;/res-ref-namegt;
lt;res-typegt;java.lang.Stringlt;/res-typegt;
lt;res-authgt;Containerlt;/res-authgt;
lt;res-sharing-scopegt;Shareablelt;/res-sharing-scopegt;
lt;/resource-refgt;
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

My original problem was how to configure Spring to use the

lt;context:load-time-weaver/gt;

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

lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans:beans xmlns=quot;schema/mvcquot;xmlns:beans=quot;schema/beansquot; xmlns:xsi=quot;2001/XMLSchema-instancequot;
xmlns: p=quot;schema/pquot; xmlns:context=quot;schema/contextquot;
xmlns:sec=quot;schema/securityquot;
xsi:schemaLocation=quot;       schema/beans        schem...-beans-3.1.xsd       schema/context        schema/context/spring-context-3.1.xsdquot;   profile=quot;tomcatquot;gt;
lt;context:load-time-weaver/gt;lt;/beans:beansgt;

projectname-config.xml

lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans:beans xmlns=quot;schema/mvcquot;xmlns:beans=quot;schema/beansquot; xmlns:xsi=quot;2001/XMLSchema-instancequot;
xmlns: p=quot;schema/pquot; xmlns:context=quot;schema/contextquot;
xmlns:sec=quot;schema/securityquot;
xsi:schemaLocation=quot;       schema/beans        schem...-beans-3.1.xsd       schema/context        schem...ontext-3.1.xsd        schema/mvc        schema/mvc/spring-mvc-3.1.xsdquot;  default-autowire=quot;byNamequot;gt;
...Note: NO PROFILE, so this is active on all servers

In my case the web.xml has

lt;context-paramgt;lt;param-namegt;contextConfigLocationlt;/param-namegt;
lt;param-valuegt;/WEB-INF/application-security.xml, /WEB-INF/projectname-config.xml, /WEB-INF/projectname-config-weaver.xmllt;/param-valuegt;
lt;/context-paramgt;

In the tomcat VM args I add
-Dspring.profiles.active=tomcat
¥
Back Forum Reply New