从context.xml加载Bean属性值 [英] Loading Bean property values from context.xml
问题描述
目前我们正在按以下方式从属性文件加载我们的JDBC源代码:
Currently we are loading our JDBC source values from properties file as per following:
<context:property-placeholder location="classpath:master.properties" ignore-unresolvable="true" />
<bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass" value="${database.driver}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
<property name="idleConnectionTestPeriod" value="60" />
<property name="idleMaxAge" value="240" />
<property name="maxConnectionsPerPartition" value="2" />
<property name="minConnectionsPerPartition" value="2" />
<property name="partitionCount" value="3" />
<property name="acquireIncrement" value="10" />
<property name="statementsCacheSize" value="50" />
<property name="releaseHelperThreads" value="3" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
scope="singleton">
<property name="targetDataSource">
<ref local="mainDataSource" />
</property>
</bean>
此段。适用于基于类路径的app.properties文件并摆脱app.properties。
This seg. works fine with classpath based app.properties file and get rid of app.properties.
我们想从context.xml加载这些值(放在<$ c中) $ c> META-INF 或 $ CATALINA_HOME / conf / context.xml
)。它将帮助我们在prod / staging服务器上加载正确的值。
We would like to load this values from context.xml (either placed in META-INF
or $CATALINA_HOME/conf/context.xml
). It will help us load proper values at prod/staging servers.
将会感谢任何帮助或替代方法/建议。 (如果已经回答了类似的问题,请分享链接)
谢谢!
Will appreciate any help or alternate method/suggestions. (If similar question is already answered, please share the link) Thanks!
推荐答案
正如Alan Hay所说,您可以将数据源配置外部化为Tomcat自己的context.xml,然后让Spring执行JNDI查找检索它。这是我在一些项目中经常使用的方法。
As Alan Hay mentioned you could externalize the datasource configuration into Tomcat's own context.xml and then have Spring do a JNDI lookup to retrieve it. This is an approach that I've commonly used on some of the projects I've worked on.
你需要实现的目标是:
1。将数据源配置添加到$ CATALINA_HOME / conf / context.xml
<GlobalNamingResources>
<Resource type="javax.sql.DataSource"
name="dsName"
factory="com.jolbox.bonecp.BoneCPDataSource"
driverClassName="your.driver.classname"
jdbcUrl="your:driver:url"
username="username"
password="password"
idleMaxAge="240"
idleConnectionTestPeriod="60"
partitionCount="3"
acquireIncrement="10"
maxConnectionsPerPartition="2"
minConnectionsPerPartition="2"
statementsCacheSize="50"
releaseHelperThreads="3" />
</GlobalNamingResources>
2。在应用程序的META-INF / context.xml中添加资源链接
<Context path="/YourApp">
<ResourceLink description="Datasource for YourApp"
global="jdbc/dsName"
name="jdbc/dsName"
type="javax.sql.DataSource" />
</Context>
3。修改Spring配置以在JNDI中查找数据源
<beans xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee classpath:/org/springframework/ejb/config/spring-jee-3.0.xsd">
<jee:jndi-lookup id="dataSource"
jndi-name="java:comp/env/jdbc/dsName" />
4。移动驱动程序和数据源jar
由于数据源配置现在是容器管理的,因此您应该将数据库驱动程序和数据源jar放入$ CATALINA_HOME / lib中,这样它们就可以了Tomcat在创建数据源时可用。这些jar不再需要驻留在应用程序的WEB-INF / lib中。
Since the datasource configuration is now container managed, you should place the database driver and datasource jars into $CATALINA_HOME/lib so they are available to Tomcat when it creates the datasource. These jars should no longer need to reside in the WEB-INF/lib of your application.
这篇关于从context.xml加载Bean属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!