Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

We added Springframework to a legacy application. We encountered places where we needed access to Springframework beans by non-Spring beans. The Spring context was loaded by:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

The appeared to work fine, but what we did not realize until we looked at the logging was that the Spring context was refreshed every time this was called. This is a no-no when you have over 1000 classes under packages to be scanned.

The correct way is by:

ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

This worked for the Servlet API objects. We had some POJOs that did not have access to the Servlet API. We were already performing special processing via a filter to add various objects to the current context by using ThreadLocal.

April 4th, 2012

Posted In: Java, Spring

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By :