开发者

How to load configuration parameters from XML files into Spring MVC Controllers?

开发者 https://www.devze.com 2023-03-09 22:38 出处:网络
I\'ve noticed that I can load a config parameter into a bean using something like this in application-context.xml:

I've noticed that I can load a config parameter into a bean using something like this in application-context.xml:

<beans:bean id="foo" class="com.foo.FooBean">
    <beans:property name="foo" value="${foo}" />
</beans:bean>

What about if I want to acce开发者_StackOverflow社区ss the foo value in a Controller without instantiating a bean? Is there a way to do that?


You can use the @Value annotation with util:properties

<util:properties id="props" location="classpath:com/foo/bar/props.properties"/>

And in your controller class, assuming you have a property with key 'foo':

@Value("#{props.foo}")
public void setFoo(String foo) {
    this.foo = foo;
}


You can use PropertyPlaceholderConfigurer

Please refer to this link to read more about it

http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/beans.html#beans-factory-placeholderconfigurer

0

精彩评论

暂无评论...
验证码 换一张
取 消