How to declare a parent application context - spring

I find myself using two identical beans in my applicationContext.xml and my applicationContext-test.xml. I'd like my test context to be able to inherit from my app context, to avoid repeating myself.
I've seen plenty of material indicating that you can declare a parent application context and reference beans from that context, but I can't find a useful example. Can anyone help?
Update
As some background info, my normal application context is being loaded in web.xml:
<context-param>
<description>Application Contexts for Spring</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
My test application context is loaded in my unit tests:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = "/applicationContext-test.xml")
So let's say I have a bean in my regular context:
<bean name="someBean" class="com.foo.MyClass" />
Then, in my test application context, I'd like to refer to this bean. How do I do it?
Update
Per skaffman's suggestion, I've moved the bean into a SharedBeans.xml file and imported it into my applicationContext.xml. However, this causes a SAXParser exception:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:SharedBeans.xml]
Offending resource: ServletContext resource [/WEB-INF/classes/applicationContext.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from class path resource [SharedBeans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'bean'.
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
I can't be sure what I'm doing wrong. The bean was working fine in my context file, and all I did was cut and paste into the new file. Here are the contents of SharedBeans.xml in its entirety:
<bean name="properties" class="com.foo.Properties">
<constructor-arg><value>${module.name}</value></constructor-arg>
<constructor-arg><value>${businessUnit}</value></constructor-arg>
<constructor-arg><value>${product}</value></constructor-arg>
<constructor-arg><value>${env}</value></constructor-arg>
<constructor-arg><value>${machineName}</value></constructor-arg>
<constructor-arg><value>${collectionSet.company}</value></constructor-arg>
<constructor-arg><value>${route.tag}</value></constructor-arg>
<constructor-arg><value>${timeout}</value></constructor-arg>
</bean>

This doesn't strike me as a particularly good use-case for a parent context, which is useful mainly to set up a hierarchy (e.g. one root webapp context, multiple child servlet contexts).
For your situation, it's going to be simpler and easier to understand if you just extract the common bean definitions into a separate file, and then <import> them into each context file that needs it. You could do this with parent-child contexts, but it's going to be harder to understand, unnecessarily so.
OK, so an example, put your shared bean definition into a file called shared-beans.xml, and put it (for now) at the top-level of your classpath, containing:
<bean name="someBean" class="com.foo.MyClass" />
Then, inside applicationContext-test.xml and /WEB-INF/classes/applicationContext.xml, add the following:
<import resource="classpath:/shared-beans.xml" />
All of the bean definitions in shared-beans.xml will now be imported into each app context. You don't get a third app-context by doing this, you just import the bean definitions from another file.

You can move your common declaration to the separate XML file and place it in classpath (for easy access from test). Then you can do the following:
<context-param>
<description>Application Contexts for Spring</description>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
classpath:/common-beans.xml
</param-value>
</context-param>
.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(
locations = {"/applicationContext-test.xml", "common-beans.xml"})
You can also include common-beans.xml from both contexts using <import>, as suggested by skaffman.

Related

ServletContext in Spring Application

I have one simple question.
If web.xml web application descriptor like this.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml,
/WEB-INF/anotherContext.xml,
/WEB-INF/another2Context.xml
</param-value>
</context-param>
ContextLoaderListener create three differents ServletContexts or three differents ServletContext childs inside one general parent ServletContext?
dispatcher-servlet.xml configuration of DispatcherServlet is another child than others three contexts above?
SOLUTION
I have been investigating about this area, I have created one example application, and every xml files from make one ServletContext only, the same root ServletContext application created by ContextLoaderListener.
maybe you can take a look at this old answer, have a great explanation.
Namespace vs contextConfigLocation Spring init parameters in web.xml
I hope it helps.

Spring XML Initialization order and Bean overridden

I am trying to understand a web system using spring and I can not debug it.
Now I am confused by the order of the XML initialization.
Support I have an web.xml which is like this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/com/pathA/**/applicationContext*.xml,classpath*:/com/pathB/**/applicationContext*.xml
</param-value>
</context-param>
and in /com/pathA I have some xml files that define some beans with the same name.
In /com/pathB I also have some xml files that define beans with the same name as in com/pathA's xml file.
I know that spring framework will use the last bean definition by default.But I can't
find the order of the xml files initialization.
Here is the beans' definition:
/com/pathA/applicationContextOne.xml
<bean name="/testBean" class="com.TestActionOne">
</bean>
/com/pathA/applicationContextTwo.xml
<bean name="/testBean" class="com.TestActionTwo">
</bean>
/com/pathB/applicationContextThree.xml
<bean name="/testBean" class="com.TestActionThree">
</bean>
can anyone can tell me the initialization order of the differnet xml files in /com/pathA/
and the initialization order of the xml files between com/pathA/ and com/pathB?
I have written some samples in local pc (Windows and AIX)to simulate the initialization order and I found the result below.
1.the initialization order of files in different path depends on the order written in
classpath*:/com/pathA/*/applicationContext.xml
on my question, the [applicationContext*.xml] files in pathA will be initialized first and then in pathB.
2.the initialization order of files in the same path is the order of the file name
for example,if in pathA there are [applicatinContextOne.xml] and [applicationContextTwo.xml],then [applicatinContextOne.xml] will be initialized first and then [applicationContextTwo.xml].
I tried this under both Windows and AIX, and it returns the same result.
Hope this will be helpful.

Using multiple applicationContexts with duplicate bean ids from different jar files in a webApplication

I have bean1 and bean2 defined in applicationContext1 in jar1 and have bean1 (of course a different class) and bean3 defined in applicationContext2 in jar2
I need to use both jar1 and jar2 in my webapplication, which also has an applicationContext3.
I use the below entries in web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext1.xml
<param-value>applicationContext2.xml
<param-value>/WEB-INF/properties/application/applicationContext3.xml
</param-value>
</context-param>
Is there a way to prefix the applicationContext IDs so that they dont step over each other;
for eg:
in applicationContext3 can I have
<bean id="myBean" >
<property name="bean1" ref="ac1:bean1">
<property name="bean2" ref="ac2:bean1">
</bean>
Thanks in advance
Maybe you could arrange it somehow (Spring is very flexible and powerful) but I think you should clean up a little your contexts.
If you are in total control of both libraries and the main app, I would recommend creating only a main applicationContext and copying the relevant beans from the JARs.
Other good solution is to separate every applicationContext in context modules, like repository-context.xml, business-context.xml, etc and reference only the ones you need, so you have more control over what is getting loaded.
Also, take a look at this:
http://www.gridshore.nl/2008/05/13/spring-application-context-loading-tricks/

Reading 1 bean from other bean defined in different xml file in different directory

How can we read one from other bean defined in a different xml file in a different directory. For example, if there is a bean defined in
src\main\resources\serviceconfig\org\project\core_spring\file1.xml
that needs to reference another bean defined in
src\main\java\org\project\core\commons\persistence_spring\file2.xml
Is there any way to reference it or to import the resources of file2.xml into file1.xml?
Specifying the whole relative classpath as in
<import resource="\org\project\core\commons\persistence_spring\file2.xml" />
solved the issue for me.
Still haven't tried the answer given by Chao though. Would be great if that one works because entering relative path and importing some resource everytime I need to refer to it can be quite cumbersome.
You can import file2.xml into file1.xml. Using
<import resource="classpath:file2.xml" />
After importing you can use bean defined in file2.xml in file1.xml.
you can use the <context-param> tag in web.xml to include multiple bean definition files in your application. You can refer to both contexts both on the classpath or in your web folder. and you will be able to use the beans defined in those files as if they were defined in one file.
For example:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/com/your-company/file1.xml
/WEB-INF/file2.xml
</param-value>
</context-param>

Can Spring Webflow define beans within flow.xml definitions?

I'm defining a lot of flows and each of my flows has a lot of actions within its states.
The namespace seems to be getting fairly crowded now, so I'm wondering if it's possible to define the spring beans for flow actions from within the flow.xml
or some other way such that it's visible to the flow, but not to other flows, but still has access to the greater spring context (for things such as service injections)
You have 1 spring context and therefore you can't have beans invisible for each other. That said, you can put different beans wit different ids in different xmls, using either:
in web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/flow1.xml,/WEB-INF/flow2.xml</param-value>
</context-param>
or in applicationContext.xml (your flowX.xml should be under /WEB-INF/classes - i.e. the root of the classpath):
<import resource="classpath*:/flow1.xml" />
<import resource="classpath*:/flow2.xml" />

Resources