【问题标题】:how to configure properties for mvc spring bean controller?如何为 mvc spring bean 控制器配置属性?
【发布时间】:2011-07-14 15:20:49
【问题描述】:

有没有特殊的方法可以做到这一点? 我得到的是:

  1. config.properties 与 param.key=value
  2. 带有读取配置的 ContextLoaderListener 的 web.xml
  3. pages-servlet.xml 定义 servlet bean。

我想要的是使用 param.key 配置 pages-servlet.xml 中的 bean 之一。 我在 xml 中使用 <property name="myField" value="${param.key}"/>,但我看到该字段配置为 ${param.key} 而不是“值”。

配置bean的正确方法是什么?

好的,我通过将定义配置 bean 的应用程序上下文文件导入 pages-servlet.xml 来解决它。 它有效,但似乎非常错误。

【问题讨论】:

  • Spring 框架有相当详细的文档,你读过用户指南吗? springframework.org/documentation
  • 是的,但找不到答案。如果您能将我指向正确的部分而不是提供一般链接,我将不胜感激。

标签: java model-view-controller spring properties controller


【解决方案1】:

属性占位符就是你想要的。

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:property-placeholder location="classpath:/config.properties" />
    <bean id="mybean" class="...">
        <property name="xxx" value="${prop.value}" />
    </bean>
</beans>

【讨论】:

  • 是的(+1),这是链接:static.springsource.org/spring/docs/3.0.x/…
  • 这正是我正在使用的。尽管有 ContextLoaderListener,但它是在 web 项目的库之一中定义的,并且由于某种原因在 servlet 上下文中无法看到,就像所有其他 bean 一样。在我将配置 bean 导入 servlet 上下文文件后,它就起作用了。
猜你喜欢
  • 1970-01-01
  • 2022-08-20
  • 1970-01-01
  • 2016-02-19
  • 2016-11-18
  • 2019-01-07
  • 2020-11-24
  • 1970-01-01
  • 2011-02-27
相关资源
最近更新 更多