Reputation: 563
I have a big web application. As part of optimizing the code, I've split them in to three modules.
Module 1 : Web
Module 2 : driver module
Module 3 : Reporting module
Here Module 1 & Module 3 are spring projects, where as the module 2 is currently pure Java module.
I want to access the Module 3(which is in spring) through Module 2.
App context xmls are present for module 1 and module 3. (say m1.xml and m3.xml)
I've included m3.xml in m1.xml as
<import resource="classpath*:m3.xml" />
In Module 2, I'm trying to introduce autowiring reference to classes in Module 3. But the first usage of the autowired field throws a Null Pointer exception.
In component scan, I've added the base package, so that it will be able to identify the class.
Can any one guide me how to rewrite Module 2 to fix this issue (ApplicationContext xml ? etc)
I'm using spring 3.1
Upvotes: 1
Views: 1135
Reputation: 563
I figured out the issue !
Actually from Module-1(webapp), I'm calling the driver module and from there calling the reporting modules.
The issue was that from driver to report, I'm calling using new references (new Report() ), so these are no longer managed by spring container. Normally objects managed by spring container can only be wired automatically. By default, the Autowired references in new() created classes will not be autowired.
The issue can fix in two ways
http://seniorjava.wordpress.com/2013/04/03/spring-configurable-magic/ (Spring - @Configurable)
OR
http://sujitpal.blogspot.in/2007/03/accessing-spring-beans-from-legacy-code.html (share the app context through static methods. This has side effects on unit testing.)
Upvotes: 1