Reputation: 13342
How to import one spring-context from another module's spring context?
module2 --uses--> module1
I have module1 and module2 (in my maven model), both are spring apps. Both projects are located on the same parent directory.
In module1 I have 'module1/Bean1' class (wich is a spring-bean)
The moodule2 is supposed to use module1. So, in the module2/src/main/resources/context2.xml I try to put 'reference' to context1 like this:
context2.xml:
<import resource="classpath*:module1/resources/context1.xml" />
So, I'm expecting to use this code in the module2:
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] {"context2.xml"});
Bean1 bean1 = (Bean1) appContext.getBean("bean1"); // It uses bean defined in the module1
But now I have an exception: No bean named 'bean1' is defined
-- context1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="module1"/> <!-- for @Component, @Service, @Repository being scanned in particular folder -->
Upvotes: 2
Views: 1295
Reputation: 179
If you context1.xml is under /module1/src/main/resources/, instead of
<import resource="classpath*:module1/resources/context1.xml" />
You should use
<import resource="classpath*:context1.xml" />
Upvotes: 1