Marko Vranjkovic
Marko Vranjkovic

Reputation: 6877

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?

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?

Upvotes: 3

Views: 4579

Answers (2)

Debojit Saikia
Debojit Saikia

Reputation: 10632

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>

Upvotes: 8

Dirk Lachowski
Dirk Lachowski

Reputation: 3151

  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 {
    ...
}

Upvotes: 0

Related Questions