简体   繁体   English

如何将 XML 文件中的配置参数加载到 Spring MVC 控制器中?

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

I've noticed that I can load a config parameter into a bean using something like this in application-context.xml :我注意到我可以在application-context.xml中使用类似的东西将配置参数加载到 bean 中:

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

What about if I want to access the foo value in a Controller without instantiating a bean?如果我想在不实例化 bean 的情况下访问 Controller 中的foo值怎么办? Is there a way to do that?有没有办法做到这一点?

You can use the @Value annotation with util:properties您可以将@Value注释与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':在您的 controller class 中,假设您有一个带有键“foo”的属性:

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

You can use PropertyPlaceholderConfigurer您可以使用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 http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/beans.html#beans-factory-placeholderconfigurer

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

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