Announcement Announcement Module
Collapse
No announcement yet.
Get context-param in applicationContext.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get context-param in applicationContext.xml

    Is it possible (in applicationContext.xml) to get value of a context-param specified in web.xml?

  • #2
    Provide ServletContext to your bean (e.g. via ServletContextAware callback) and get the attribute using servlet api.

    Comment


    • #3
      This would work in bean code, but what I actually want to do is to take a file specified by context-param in web.xml and load it using

      Code:
      <context:property-placeholder location="path_specified_by_context-param" />

      Comment


      • #4
        Originally posted by adeon View Post
        This would work in bean code, but what I actually want to do is to take a file specified by context-param in web.xml and load it using

        Code:
        <context:property-placeholder location="path_specified_by_context-param" />
        Such facility is not offered by the spring out of the box. However, it's very easy to implement it yourself. You should perform the following:
        1. Subclass PropertyPlaceholderConfigurer class;
        2. Make the subclass implement ServletContextAware;
        3. Instantiate Properties on the basis of the provided context config param file name;;
        4. Override resolvePlaceholder() method in order to call super.resolvePlaceholder() and fallback to the Properties you instantiated before;

        Comment


        • #5
          I know this is a bit old, but as it comes up high in search results for "access context-param in Spring", I thought I would just give an easier solution. First, in your Spring applications "applicationContext.xml" (or whatever you named it, add a property placeholder like so:

          <contextroperty-placeholder local-override="true" ignore-resource-not-found="true"/> An optional parameter of "location" could be added if you would also like to load some values found in .properties files. ( location="WEB-INF/my.properties" for example).
          The important attribute to remember is the 'local-override="true"' attribute, which tells the place holder to use context parameters if it can't find anything in the loaded properties files, which in this case you never specified.
          Then in your constructors and setters you can do the following, using the @Value annotation and some handy expression language:

          @Component public class AllMine{ public AllMine(@Value("${stuff}") String stuff){ //Do stuff } } This method has the added benefit of abstracting away from the ServletContext, and gives you the ability to override default context-param values with custom values in a properties file.

          Comment

          Working...
          X