如何将属性值注入到注释配置的spring mvc 3.0控制器中 [英] How can I inject a property value into an annotation configured spring mvc 3.0 controller

查看:203
本文介绍了如何将属性值注入到注释配置的spring mvc 3.0控制器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:我正在使用Spring 3.0

First: I'm using Spring 3.0

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

I have a problem when configuring my controller class. The controller uses a web service which I want to define the endpoint address using a .properties file.

@Controller
public class SupportController {

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

    ...

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

In my application context xml-file, I've defined this:

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

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

I've been reading the documentation, trying different approaches (like adding prefix systemProperties.),but I keep getting an error message telling me that it doesn't exist.


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

Field or property 'url' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'






好的。我已经弄清楚了。


Ok. I've figured it out.

现在,在控制器中:

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

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

Then in the context configuration I have this "helper bean":

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


推荐答案

这也应该有效:

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

这篇关于如何将属性值注入到注释配置的spring mvc 3.0控制器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆