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
  • 16,038
  • 10
  • 74
  • 104
Marko Vranjkovic
  • 6,481
  • 7
  • 49
  • 68

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
  • 85,076
  • 16
  • 154
  • 332
Debojit Saikia
  • 10,532
  • 3
  • 35
  • 46
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,121
  • 4
  • 40
  • 66
  • 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