java - 在 contextConfigLocation 参数中配置的 xml 中替换占位符的问题
问题描述
当我将占位符放入 web.xml 时,它会被 catalina.properties 中定义的值或 vm 选项中的 -D 参数正确替换。但是当我将占位符放入由 ContextLoaderListener 加载的 xml 时,它不起作用。
web.xml 片段:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
${placeholder}, <!-- WORKS -->
/WEB-INF/applicationContext.xml <!-- DON'T WORK, WHEN PLACEHOLDER IS DEFINED INSIDE XML -->
</param-value>
</context-param>
解决方案
问题解决了。contextConfigLocation 中定义的xml由spring加载,需要从org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类中定义bean。属性文件可以为空。值将从 catalina.properties 或 vm 选项加载。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:example.properties</value>
</property>
</bean>
推荐阅读
- kubernetes-ingress - 使用 Kong-Ingress-Controller 部署 Kong 时如何访问 Kong 管理 API
- php - 表单页面未重定向到 Laravel 中的所需页面
- java - 扫描“打电话回家”的依赖罐?
- php - 如何将 int 时区转换为字符串时区
- bash - 为什么我的变量中有这个空间?我怎样才能修剪它?
- c - 此代码是否会造成内存泄漏以及如何对其进行测试
- c++ - C 风格转换参考
- angular - 如何更改角度图表边框?
- firebase - 应用程序的 Xamarin Firebase 身份验证
- sql - 在 SQL 中选择数值为十六进制 16