Spring PropertySource and Environment

Brent Putman putmanb at georgetown.edu
Fri May 2 14:49:19 EDT 2014


I promised to send something out about this on today's call.

The main documentation which lead me to looking at this for another
project is here [1], which is linked to from the official Spring docs
[2].  (I find it slightly odd that they just link out to a blog article,
rather than actually having this in the docs directly).

There they talk about the general use of PropertySources and at the
bottom, how you would use them in a web environment via an
ApplicationContextInitializer impl.

So what I did is here [3].  It's very simple.  There I hardcoded a
reference to an external properties file, but there's probably multiple
ways to abstract that if that is undesirable.

Then it just gets called to init the root context by registering it in
web.xml like so:

<context-param>
     <param-name>contextInitializerClasses</param-name>
    
<param-value>edu.georgetown.blackboard.coursecombiner.b2.support.CourseCombinerApplicationContextInitializer</param-value>
 </context-param>


The only thing I'm not 100% certain about is whether the properties so
defined are inherited by child app contexts.  But from what I just read
I think that they effectively are, because (I think) children inherit
the Environment instance from the parent, which includes the
PropertySources.  I can try and find an easy way to test, unless we have
some easy way to do in the testbed, etc.

Anyway, I don't know if this is useful, or solves any problems or makes
things cleaner, but worth looking at.  One advantage I think is that you
could then just have generic boilerplate:

<context:property-placeholder />

declared once in each app context config XML, without referencing any
specific 'location', etc.

And of course with the initializer one could things to the context other
than just setting PropertySources.

******************

Also, in looking at this just now, I got excited because I came across
this in the spring-web ContextLoader class:

/**
 * Config param for global {@link ApplicationContextInitializer} classes
to use
 * for initializing all web application contexts in the current
application: {@value}
 * @see #customizeContext(ServletContext, ConfigurableWebApplicationContext)
 */
public static final String GLOBAL_INITIALIZER_CLASSES_PARAM =
"globalInitializerClasses";


which is distinct from the contextInitializerClasses that is in the
wiki, and that I used above.  I thought it might allow custom init of
the non-web contexts, children, etc.  But I can't actually see that they
are doing anything different with it, those seem to just get lumped in
with the contextInitializerClasses.   It's possible I'm just missing
something.  Some discussion here [4].

I haven't yet found anything like a "listener" type of component which
can apply ApplicationContextInitializers in some general way, but in
reading [4] I did just stumble upon the fact that the Spring MVC
DispatcherServlet does also support the use of
ApplicationContextInitializers, using servlet config param(s).  I have
not tried this at all.  Don't know if we have a need for customizing
that context, but looks like you could do it.

(In fact, this is probably one way you could spin up and attach Spring
contexts as direct children of the root web application context, and
then make one of them be the parent of the DispatcherServlet's context -
instead of the web root context.  But I think we've decided not to go
there.)

It would be nice if SWF had a similar hook to invoke context
initializers on the contexts it creates for flows, etc, but I haven't
gotten that far.  I'd be mildly surprised if they did...

--Brent




[1]
http://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/ 


[2]
http://docs.spring.io/spring/docs/4.0.3.RELEASE/spring-framework-reference/html/beans.html#beans-property-source-abstraction

[3]
https://www.middleware.georgetown.edu/examples/CourseCombinerApplicationContextInitializer.java

[4] https://jira.spring.io/browse/SPR-11314
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://shibboleth.net/pipermail/dev/attachments/20140502/25469d53/attachment.html 


More information about the dev mailing list