是否可以在 context.xml 中使用占位符

发布于 2024-08-07 00:11:03 字数 1351 浏览 3 评论 0 原文

我正在使用 Spring 和 struts,并且在“/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>

是否可以通过以下方式实现,

<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>

我的 applicationContext.xml 有以下内容,

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

我想获取以下值属性文件中的 jdbc.username 和 jdbc.pwd。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷遇一生 2024-08-14 00:11:03

不可能使用 Spring 的 PlaceholderPropertyConfigurer (它只替换 Spring 上下文中的值)。

但是,可以在使用 Ant。 apache.org/manual/Tasks/replace.html" rel="nofollow noreferrer">替换 任务。类似于:

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

请注意,上面将属性名称作为要替换的标记 - 例如,您需要在 context.xml 中使用“jdbc.url”而不是“${jdbc.url}”。如果绝对需要后者,则可以通过显式命名要替换为嵌套 元素的标记来实现。

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.

梦回梦里 2024-08-14 00:11:03

对于 Tomcat,您可以在服务器的 server.xml 文件中设置连接池,这样用户名/密码就位于您的 war 文件之外。以下是有关 Context 元素在 Tomcat 5.5 中的行为方式的一些信息 http://tomcat .apache.org/tomcat-5.5-doc/config/context.html

或者,您可以使用独立的 DBCP 包,并使用 jdbc.properties 替换其中的用户名/密码。例如:

<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>

For Tomcat, you can setup a connection pool in the server's server.xml file, that way the username/password is outside of your war file. Here's some info on how Context elements behave in Tomcat 5.5 http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

Alternately, you can use the standalone DBCP package from Apache from your Spring config file, and use the jdbc.properties to replace your username/password in 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文