I created a new Spring MVC project using the Spring Template Project functionality. Since 2.3 or so, the format of the configuration files has changed a bit, so that there's now this root-context.xml file created. I get the idea, you can create stuff at the root level, then any Spring servlet context that's created later essentially inherits that root context.
The problem is that auto-wiring at that root level doesn't appear to work properly. Here's a sample project I've created that illustrates the problem:
SpringMvcSandbox.zip
Both root-context.xml (at the root level) and servlet-context.xml (at the servlet level) have an import statement like this:
For the root, the import path is different, but otherwise it's the same. If you comment out the import at the root level, UNCOMMENT the import at the servlet level, and run the app, everything works: the DateService gets wired into the HomeService and HomeController will display a message.HTML Code:<import resource="../services/service-context.xml"/>
Now reverse that: UNCOMMENT the import at the ROOT level, COMMENT the import at the SERVLET level, and run it. You'll get a NPE where the HomeService tries to reference the DateService. You can make it work by uncommenting the direct property reference in the service-context.xml (which is commented out in my sample code), so it's not that you can't cross-reference beans in the root context, just that that never gets autowired.
This may be by design, may be known, or whatever, but I just spent a while totally confused as to why my really simple autowire references weren't working! Is this because the root context isn't actually instantiated until referenced by a child or... I just don't know why the autowire isn't performed. Any ideas?


Reply With Quote
