【问题标题】:How do i get actual value instead of placeholder while loading bean?加载bean时如何获取实际值而不是占位符?
【发布时间】:2012-07-31 19:13:03
【问题描述】:

在加载 bean 时,我得到的是占位符而不是其属性值。

属性文件

tm.web.keystore.key.password=WaheeD
tm.web.tcp.backlog=1024
tm.web.min.jetty.threads=8
tm.web.max.jetty.threads=25

appcontext.xml 文件

<bean class="com.intel.ssg.mconsole.core.web.WebServer" id="webServer">
    <property name="port" value="${tm.web.port}" />
    <property name="address" value="${tm.web.address}" />
    <property name="warLocation" value="${tm.home}/mconsole.war" />
    <property name="secure" value="${tm.web.secure}" />
    <property name="keystoreLocation" value="${tm.web.keystore.location}" />
    <property name="keystorePassword" value="WaheeD" />
    <property name="keyPassword" value="${tm.web.keystore.key.password}" />
    <property name="tcpBacklog" value="${tm.web.tcp.backlog}" />
    <property name="minJettyThreads" value="${tm.web.min.jetty.threads}" />
    <property name="maxJettyThreads" value="${tm.web.max.jetty.threads}" />
</bean>

通过编组器加载它

 try {
     FileInputStream fis = new FileInputStream(getAppContextFile());
     try {
         return (Beans) JAXBUtil.getUnmarshaller().unmarshal(fis);
     } finally {
         fis.close();
     }

在 Beans 中,对于 bean webServer 端口,我得到的值是 ${tm.weberver.port},而不是它的确切值。假设是 8443 端口。

【问题讨论】:

  • 为什么你期望属性注入会自动发生而无需实例化 ApplicationContext?在您的示例中,您只是读取 XML 文件,仅此而已 - 对吗?
  • 对..那么如何使用编组器将 appcontext 加载到 bean 中?
  • 别这样,使用appcontext加载器,看我下面的帖子。

标签: java spring


【解决方案1】:

您发布了一个没有任何端口号属性的属性文件,一个使用${tm.web.port} 的上下文文件,并且您的文字说您正在使用${tm.weberver.port}。

我发现您的错误可能出现在三个地方。这是假设您实际上正在设置其他属性。如果没有,请参阅 Andrey 的评论。

【讨论】:

  • 属性文件中有tm.web.port,只是我这里没有写所有代码。 ${tm.webserver.port}。是打字错误..
【解决方案2】:

您不需要从 ApplicationContext.xml 中明确加载 bean。

确保存在以下内容:

【讨论】:

  • 感谢您的回复。我已经完成了所有这些,并且我的应用程序已正常运行。没有问题。
  • 我想使用 Rest 读取属性文件。我为此打开了以下 REST 调用:'@RequestMapping(value = "/beans/{id}/{name}", method = RequestMethod.GET) @ResponseBody public String getBeanPropertyValue(@PathVariable("id") String beanId , @PathVariable("name") String propertyName)'....假设一个请求知道端口 i,例如 localhost:8443/admin/webServer/port,该值应该是 8443,但现在我正在获取它的占位符。
猜你喜欢
  • 2021-12-16
  • 2021-12-14
  • 2019-07-13
  • 1970-01-01
  • 1970-01-01
  • 2010-11-09
  • 2022-11-17
  • 2022-11-20
  • 1970-01-01
相关资源
最近更新 更多