Is it possible to use placeholder in context.xml

I use 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 as follows:

 <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 context.xml application has the following:

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

I want to get the values ​​of jdbc.username and jdbc.pwd from a properties file.

+4
java spring struts
09 Oct '09 at 4:04
source share
2 answers

It is not possible to use Spring PlaceholderPropertyConfigurer (which replaces only the values ​​inside the Spring context).

However, you can use Ant during the build process using Replace . Something like:

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

Please note that the above property names are used as tokens to be replaced - for example, you will need to use "jdbc.url" and not "$ {jdbc.url}" in your context. xml. If the latter is absolutely necessary, this can be achieved by explicitly specifying tokens that should be replaced as nested <replacefilter> elements.

+1
Oct 09 '09 at 4:38
source share

For Tomcat, you can configure the connection pool in the server.xml file of the server, so the username / password is outside your military file. Here is some information on how context elements behave in Tomcat 5.5 http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

Alternatively, you can use the standalone DBCP package from Apache from your Spring configuration file and use jdbc.properties to replace your username / password there. For example:

 <context:property-placeholder location="jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> <property name="initialSize"> <value>30</value> </property> <property name="maxActive"> <value>100</value> </property> <property name="maxWait"> <value>10000</value> </property> </bean> 
+1
Oct. 10 '09 at 12:02
source share



All Articles