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

SyA*_*yAu 5 java spring struts context.xml

我正在使用 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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

我的 applicationContext.xml 有以下内容,

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/xxx" />
</bean>
Run Code Online (Sandbox Code Playgroud)

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

Chs*_*y76 1

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

但是,可以在使用Replace任务的构建过程中使用Ant。就像是:

<replace file="META-INF/context.xml" replacefilterfile="my.properties" />
Run Code Online (Sandbox Code Playgroud)

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