简体   繁体   English

如何将属性值注入到已配置注释的Spring MVC 3.0控制器中

[英]How can I inject a property value into an annotation configured spring mvc 3.0 controller

First: I'm using Spring 3.0 第一:我正在使用Spring 3.0

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. 控制器使用Web服务,我想使用.properties文件定义端点地址。

@Controller
public class SupportController {

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

    ...

In my application context xml-file, I've defined this: 在我的应用程序上下文xml文件中,我定义了以下内容:

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

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. 我一直在阅读文档,尝试使用不同的方法(例如添加前缀systemProperties。 ),但是我不断收到一条错误消息,告诉我它不存在。

Field or property 'url' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' 在类型为“ org.springframework.beans.factory.config.BeanExpressionContext”的对象上找不到字段或属性“ url”


Ok. 好。 I've figured it out. 我知道了。

Now, in the controller: 现在,在控制器中:

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

Then in the context configuration I have this "helper bean": 然后在上下文配置中,我有这个“帮助器bean”:

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

This should work, too: 这也应该起作用:

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

I have this configuration and it works fine: 我有此配置,它工作正常:

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

and I iniejct the property in this way 我以这种方式创造财产

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

the field is correctly initialized to "DEBUG" value. 该字段已正确初始化为“ DEBUG”值。

you should check that the 您应该检查

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

is defined in webmvc-config.xml where you create instances of the @Controllers 在webmvc-config.xml中定义,您可以在其中创建@Controllers的实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM