Search code examples
javaspringstrutscontext.xml

Is it possible to use placeholder in context.xml


I'm using Spring and struts and have the following entry in '/META-INF/context.xml'

<Context cachingAllowed="false" useHttpOnly="true">
<Resource name="jdbc/xxx" auth="Container" type="javax.sql.DataSource"
           factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="xxxxx" password="xxxxx"
           driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
           url="jdbc:sqlserver://xxx:1433;databaseName=xxx;"/>
</Context>

Is it possible to implement in the following way,

<Context cachingAllowed="false" useHttpOnly="true">
   <Resource name="jdbc/xxx" auth="Container" type="javax.sql.DataSource"
               factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="${jdbc.username}" password="${jdbc.pwd}"
               driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
               url="${jdbc.url}"/>
 </Context>

My applicationContext.xml has got the following,

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/xxx" />
</bean>

I want to pick up the values of jdbc.username and jdbc.pwd from a properties file.


Solution

  • It's not possible using Spring's PlaceholderPropertyConfigurer (which only replaces values inside Spring context).

    It is, however, possible using Ant during build process using Replace task. Something like:

    <replace file="META-INF/context.xml" replacefilterfile="my.properties" />
    

    Note that the above takes property names as tokens to be replaced - e.g. you'll need to use "jdbc.url" and not "${jdbc.url}" in your context.xml. If latter is absolutely required it can be achieved by explicitly naming tokens to be replaced as nested <replacefilter> elements.