问题描述
限时送ChatGPT账号..我在 context.xml 中有一个 DataSource 配置.是否可以不在该文件中硬编码数据库参数?例如,使用外部属性文件,并从中加载参数?
I have a DataSource configuration in context.xml. Is it possible not to hard-code database parameters in that file? For example, use an external properties file, and load the parameters from it?
类似这样的东西:
context.xml:
context.xml:
<Resource
name="jdbc/myDS" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="${db.url}"
username="${db.user}"
password="${db.pwd}"
maxActive="2"
maxIdle="2"
maxWait="-1"/>
db.properties:
db.properties:
db.url=jdbc:oracle:thin:@server:1521:sid
db.user=test
db.pwd=test
推荐答案
如前所述 此处,您可以通过以下方式执行此操作.
As stated here, you could do this in the following way.
1.下载tomcat库获取接口定义,比如定义maven依赖:
1.Download tomcat library to get the interface definition, for instance by defining maven dependency:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-coyote</artifactId>
<version>7.0.47</version>
</dependency>
2.下一步是通过以下方式创建一个 com.mycompany.MyPropertyDecoder:
2.Next step is to create a com.mycompany.MyPropertyDecoder in the following way:
import org.apache.tomcat.util.IntrospectionUtils;
public class MyPropertyDecoder implements IntrospectionUtils.PropertySource {
@Override
public String getProperty(String arg0) {
//TODO read properties here
return null;
}
}
3.将 MyPropertyDecoder.class 放入 tomcat7/lib 文件夹
4.定义org.apache.tomcat.util.digester.tomcat7/conf/catalina.properties 中的 PROPERTY_SOURCE 属性如下:
3.Put MyPropertyDecoder.class into tomcat7/lib folder
4.Define org.apache.tomcat.util.digester. PROPERTY_SOURCE property at tomcat7/conf/catalina.properties as following:
org.apache.tomcat.util.digester.PROPERTY_SOURCE=com.mycompany.MyPropertyDecoder
5.用属性变量更新你的context.xml
5.Update your context.xml with properties vars
<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="${db.password}"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mysql?autoReconnect=true"
...
6.将 application.properties 文件放在您的项目/容器中
7.确保MyPropertyDecoder正确读取application.properties
8.享受吧!
6.Put application.properties file somewhere in your project/container
7.Make sure MyPropertyDecoder correctly reads application.properties
8.Enjoy!
PS 还有一个类似的方法描述了 tc Server.
PS Also there is a similar approach described for tc Server.
这篇关于外化Tomcat配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论