<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body text="#000000" bgcolor="#FFFFFF">
I promised to send something out about this on today's call.<br>
<br>
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).<br>
<br>
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.<br>
<br>
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.<br>
<br>
Then it just gets called to init the root context by registering it
in web.xml like so:<br>
<br>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<tt><context-param></tt><tt><br>
</tt><tt>
<param-name>contextInitializerClasses</param-name></tt><tt><br>
</tt><tt>
<param-value>edu.georgetown.blackboard.coursecombiner.b2.support.CourseCombinerApplicationContextInitializer</param-value></tt><tt><br>
</tt><tt> </context-param></tt><tt><br>
</tt><br>
<br>
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.<br>
<br>
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:<br>
<tt><br>
</tt><tt><context:property-placeholder /></tt><br>
<br>
declared once in each app context config XML, without referencing
any specific 'location', etc.<br>
<br>
And of course with the initializer one could things to the context
other than just setting PropertySources.<br>
<br>
******************<br>
<br>
Also, in looking at this just now, I got excited because I came
across this in the spring-web ContextLoader class:<br>
<br>
<tt>/**</tt><tt><br>
</tt><tt> * Config param for global {@link
ApplicationContextInitializer} classes to use</tt><tt><br>
</tt><tt> * for initializing all web application contexts in the
current application: {@value}</tt><tt><br>
</tt><tt> * @see #customizeContext(ServletContext,
ConfigurableWebApplicationContext)</tt><tt><br>
</tt><tt> */</tt><tt><br>
</tt><tt>public static final String GLOBAL_INITIALIZER_CLASSES_PARAM
= "globalInitializerClasses";</tt><br>
<br>
<br>
which is distinct from the <tt>contextInitializerClasses </tt>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 <tt>contextInitializerClasses.
</tt>It's possible I'm just missing something. Some discussion here
[4].<br>
<br>
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.<br>
<br>
(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.)<br>
<br>
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...<br>
<br>
--Brent<br>
<br>
<br>
<br>
<br>
[1]
<a class="moz-txt-link-freetext" href="http://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/">http://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/</a>
<br>
<br>
[2]
<a class="moz-txt-link-freetext" href="http://docs.spring.io/spring/docs/4.0.3.RELEASE/spring-framework-reference/html/beans.html#beans-property-source-abstraction">http://docs.spring.io/spring/docs/4.0.3.RELEASE/spring-framework-reference/html/beans.html#beans-property-source-abstraction</a><br>
<br>
[3]
<a class="moz-txt-link-freetext" href="https://www.middleware.georgetown.edu/examples/CourseCombinerApplicationContextInitializer.java">https://www.middleware.georgetown.edu/examples/CourseCombinerApplicationContextInitializer.java</a><br>
<br>
[4] <a class="moz-txt-link-freetext" href="https://jira.spring.io/browse/SPR-11314">https://jira.spring.io/browse/SPR-11314</a><br>
</body>
</html>