简体   繁体   English

在Spring 3.2 MVC配置中设置ServletContext的属性

[英]Set attributes of ServletContext in Spring 3.2 MVC configuration

I'm stuck on a pretty simple task: how to set ServletContext attributes in Spring MVC 3.2 configuration? 我遇到了一个非常简单的任务:如何在Spring MVC 3.2配置中设置ServletContext属性?

I found that something similar can be done with ServletContextPropertyPlaceholderConfigurer, but from Spring 3.1 this is considered as deprecated: 我发现使用ServletContextPropertyPlaceholderConfigurer可以完成类似的操作,但是从Spring 3.1开始,这被认为是弃用的:
" Deprecated . in Spring 3.1 in favor of PropertySourcesPlaceholderConfigurer in conjunction with StandardServletEnvironment. " 在Spring 3.1中 不推荐使用 PropertySourcesPlaceholderConfigurer和StandardServletEnvironment。

This doesn't tell me much, since I don't know how to do it with StandardServletEnvironment. 这并没有告诉我多少,因为我不知道如何使用StandardServletEnvironment。

Any suggestion? 有什么建议吗?

You can use ServletContextAttributeExporter for this. 您可以使用ServletContextAttributeExporter Define a ServletContextAttributeExporter bean as below in your configuration file and set its attributes property to a map of key and value pairs that you want to put into ServletContext : 在配置文件中定义ServletContextAttributeExporter bean,并将其attributes属性设置为要放入ServletContextkey and value对的映射:

<bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
    <property name="attributes">
        <map>
            <entry key="myKey" value="1" />
        </map>
    </property>
</bean>
  1. Create a *.properties file somewhere in your class path, eg /myprops.properties . 在类路径的某处创建一个* .properties文件,例如/myprops.properties
  2. Add a property-placeholder to your context-config: 将属性占位符添加到context-config:

    <context:property-placeholder location="myprops.properties"/>

or, if you are using java config: 或者,如果您使用的是java配置:

@Configuration
@PropertySource("classpath:myprops.properties")
public class ApplicationConfiguration {
    ...
}

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

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