JavaTechnical
JavaTechnical

Reputation: 9357

How to import a spring application context into another application context?

I have two spring application contexts. One is local to my application and the other is from one of the maven dependencies.

Now, my applicationContext.xml file look like this.

<import resource="classpath*:**/sample-applicationContext.xml" />

and I have <context:component-scan> in the sample-applicationContext.xml file which scans for components.

and Now, when I do the following..

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
MyClass m=ctx.getBean(MyClass.class);

Unfortunately, when I get the MyClass object, the object is created however, I see that dependencies in MyClass are not injected.

The dependencies autowired in MyClass are the beans that are scanned using the <context:component-scan> in sample-applicationContext.xml file.

Is there any way to make use of multiple application contexts present in the Maven dependencies and autowire beans in them in my project classes?

Upvotes: 6

Views: 8721

Answers (1)

Chathuranga Tennakoon
Chathuranga Tennakoon

Reputation: 2189

the following way, you can load the multiple spring applicationContext files.

ApplicationContext context = 
new ClassPathXmlApplicationContext(new String[] {"sample-applicationContext.xml",   "applicationContext.xml"});

it will be good if you can post the applicationContext.xml , sample-applicationContext.xml and MyClass code segments here.

Upvotes: 0

Related Questions