简体   繁体   中英

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

First: I'm using 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.

@Controller
public class SupportController {

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

    ...

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

<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.

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


Ok. I've figured it out.

Now, in the controller:

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

Then in the context configuration I have this "helper 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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