6

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?

JavaTechnical
  • 8,846
  • 8
  • 61
  • 97
  • Hey check out other similar question with answer [here](https://stackoverflow.com/questions/6468195/spring-import-application-context-from-another-project) – corneliouz Bett Jun 02 '20 at 11:29

2 Answers2

0

I'm not sure about maven dependencies, but you are trying to do something like a cross-context using spring. Take a look at this link: http://blog.imaginea.com/cross-context-communication-between-web-applications/

EDIT: it seems there is a way, take a look at this post: Loading spring application context files that are inside a jar in classpath

Community
  • 1
  • 1
Luke SpringWalker
  • 1,600
  • 3
  • 19
  • 33
  • that didn't work. I am getting the dependency bean object, when I call them using ApplicationContext object `ctx.getBean(MyDependencyBean.class)` but when autowiring `MyDependencyBean` in `MyClass`. It is not autowired. In `MyClass` the `MyDependencyBean` is made `transient` to avoid `NotSerializableException`. Is it the reason why the dependency is not autowired:? What is the solution? – JavaTechnical Feb 13 '15 at 16:55
0

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.

Chathuranga Tennakoon
  • 2,059
  • 1
  • 18
  • 20