Java 如何在加载bean时获得实际值而不是占位符?

Java 如何在加载bean时获得实际值而不是占位符?,java,spring,Java,Spring,加载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"> <

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

在Beans中,我得到的值为${tm.webServer.port}而不是BeanWebServer端口的精确值。假设8443端口

您发布了一个属性文件,该文件没有端口号的任何属性,一个使用
${tm.web.port}
的上下文文件,您的文本显示您正在使用
${tm.webserver.port}


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

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

确保存在以下各项:

  • 在ApplicationContext.xml中,必须为属性文件提供属性加载器,例如
  • 在应用程序中,需要应用程序上下文加载器。您当前的框架中可能内置了一个,例如中的WebApplicationContext。如果你刚开始,你可以使用
为什么您希望在不实例化ApplicationContext的情况下自动进行属性注入?在您的示例中,您只是在读取XML文件,没有其他内容-正确吗?对..那么如何使用marshaller将appcontext加载到bean中?不要这样做,使用appcontext加载程序,请参阅下面的文章。属性文件中有tm.web.port,只是我没有在这里编写所有代码${tm.webserver.port}。是一个输入错误。谢谢你的回复。我已经完成了所有这些,并且我的应用程序已经启动并正常运行。这没有问题。我想使用Rest读取属性文件。我为此打开了以下REST调用:“@RequestMapping(value=“/beans/{id}/{name}”,method=RequestMethod.GET)@ResponseBody公共字符串getBeanPropertyValue(@PathVariable(“id”)String beanId,@PathVariable(“name”)String propertyName)”……假设一个重新请求知道端口I,e,值应该是8443,但现在我得到了它的占位符。
 try {
     FileInputStream fis = new FileInputStream(getAppContextFile());
     try {
         return (Beans) JAXBUtil.getUnmarshaller().unmarshal(fis);
     } finally {
         fis.close();
     }