如何访问< context-param> Spring Controller中web.xml的值 [英] How to access <context-param> values of web.xml in Spring Controller
问题描述
我正在如下所示在我的应用程序的web.xml中定义一个上下文参数
I am defining a context-param in web.xml of my application as below
<context-param>
<param-name>baseUrl</param-name>
<param-value>http://www.abc.com/</param-value>
</context-param>
现在我想在我的控制器中使用baseUrl的值,所以我如何访问此.....?
Now i want to use the value of baseUrl in my Controller, so how i can access this.....?
请告诉我是否有人知道这件事.
Please tell me if anyone knows about this.
提前谢谢!
推荐答案
如果您使用的是Spring 3.1+,则无需执行任何特殊操作即可获取该属性.只需使用熟悉的$ {property.name}语法即可.
If you are using Spring 3.1+, you don't have to do anything special to obtain the property. Just use the familiar ${property.name} syntax.
例如,如果您拥有:
<context-param>
<param-name>property.name</param-name>
<param-value>value</param-value>
</context-param>
web.xml
或
<Parameter name="property.name" value="value" override="false"/>
在Tomcat的context.xml
然后您可以按以下方式访问它:
then you can access it like:
@Component
public class SomeBean {
@Value("${property.name}")
private String someValue;
}
之所以可行,是因为在Spring 3.1+中,部署到Servlet环境时注册的环境是StandardServletEnvironment ,它将所有与Servlet上下文相关的属性添加到不断出现的Environment
.
This works because in Spring 3.1+, the environment registered when deploying to a Servlet environment is the StandardServletEnvironment that adds all the servlet context related properties to the ever present Environment
.
这篇关于如何访问< context-param> Spring Controller中web.xml的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!