【问题标题】:How can I inject a property value into an annotation configured spring mvc 3.0 controller如何将属性值注入注释配置的 spring mvc 3.0 控制器
【发布时间】:2010-01-13 09:39:51
【问题描述】:

首先:我使用的是 Spring 3.0

我在配置控制器类时遇到问题。控制器使用 Web 服务,我想使用 .properties 文件定义端点地址。

@Controller
public class SupportController {

    @Value("#{url.webservice}") 
    private String wsEndpoint;

    ...

在我的应用程序上下文 xml 文件中,我定义了这个:

<context:property-placeholder location="/WEB-INF/*.properties" />

我一直在阅读文档,尝试不同的方法(例如添加前缀 systemProperties.),但我不断收到一条错误消息,告诉我它不存在。

字段或属性“url”不能 在“org.springframework.beans.factory.config.BeanExpressionContext”类型的对象上找到


好的。我想通了。

现在,在控制器中:

@Value("#{settings['url.webservice']")

然后在上下文配置中我有这个“帮助 bean”:

<util:properties id="settings" 
location="/WEB-INF/supportweb.properties"></util:properties>

【问题讨论】:

标签: java spring


【解决方案1】:

这也应该有效:

@Value("${url.webservice}") 
private String wsEndpoint;

【讨论】:

    【解决方案2】:

    我有这个配置,它工作正常:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
           <list>
             <value>classpath:application.properties</value>
          </list>
        </property>
    </bean>
    

    我以这种方式投资房产

    @Value("${root.log.level}") 
    private String prop;
    

    该字段已正确初始化为“DEBUG”值。

    【讨论】:

      【解决方案3】:

      你应该检查一下

      <context:property-placeholder location="/WEB-INF/*.properties" />
      

      在 webmvc-config.xml 中定义,您可以在其中创建 @Controllers 的实例

      【讨论】:

        猜你喜欢
        • 2010-09-23
        • 1970-01-01
        • 2015-07-03
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        • 1970-01-01
        • 1970-01-01
        • 2019-04-30
        相关资源
        最近更新 更多