3

I'm stuck on a pretty simple task: how to set ServletContext attributes in Spring MVC 3.2 configuration?

I found that something similar can be done with ServletContextPropertyPlaceholderConfigurer, but from Spring 3.1 this is considered as deprecated:
"Deprecated. in Spring 3.1 in favor of PropertySourcesPlaceholderConfigurer in conjunction with StandardServletEnvironment."

This doesn't tell me much, since I don't know how to do it with StandardServletEnvironment.

Any suggestion?

informatik01
  • 15,636
  • 10
  • 72
  • 102
Marko Vranjkovic
  • 5,725
  • 4
  • 44
  • 66

2 Answers2

8

You can use ServletContextAttributeExporter for this. 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:

<bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
    <property name="attributes">
        <map>
            <entry key="myKey" value="1" />
        </map>
    </property>
</bean>
Luiggi Mendoza
  • 83,472
  • 15
  • 149
  • 315
Debojit Saikia
  • 10,262
  • 2
  • 33
  • 43
0
  1. Create a *.properties file somewhere in your class path, e.g. /myprops.properties.
  2. Add a property-placeholder to your context-config:

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

or, if you are using java config:

@Configuration
@PropertySource("classpath:myprops.properties")
public class ApplicationConfiguration {
    ...
}
Dirk Lachowski
  • 3,083
  • 4
  • 40
  • 65
  • When you mean "context-config", is the `application-context.xml` right? –  Feb 13 '14 at 09:15
  • Thank you. Once I put `` in `application-context.xml`, plus `@PropertySource("classpath:myprops.properties")`, can I do `@Value()` to get the contents of `myprops.properties` for file I/O? –  Feb 13 '14 at 09:50
  • @user75782131 Not "plus", use either ` – Dirk Lachowski Feb 13 '14 at 11:16