Handler Interceptors For Annotation Based Configuration in Spring

Here’s what you need to have in your *-servlet.xml file:

<context:annotation-config />
<context:component-scan base-package="com.travelfusion.tfweb.guide.controller" />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
   <property name="interceptors">
      <ref bean="myInterceptor" />

<bean id="myInterceptor" class="com.company.MyInterceptor">
  <property name="openingTime"><value>9</value></property>
  <property name="closingTime"><value>18</value></property>

And your interceptor class something like:

public class MyInterceptor implements HandlerInterceptor {
   private int openingTime;
   private int closingTime;

   public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws    Exception {
      // TODO Auto-generated method stub

   public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws    Exception {
      // TODO Auto-generated method stub

   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(HOUR_OF_DAY);
        if (openingTime <= hour < closingTime) {
           return true;
        } else {
           return false;

Software Development Tools

I have used the tools listed below for software development, debugging, testing and for editing images for user interfaces. 🙂 Hope you find them useful too!


Altova® XMLSpy® is the industry’s best selling XML editor and XML development environment, with advanced functionality for modeling, editing, and debugging XML Schema, DTD, XSLT 1.0/2.0, XQuery, SOAP, WSDL, Office Open XML (OOXML), and more. XMLSpy provides multiple editing views, entry helpers, wizards, comprehensive validation and error-handling, and a host of debugging and testing utilities. Code gen in Java, C#, and C++, plus Visual Studio and Eclipse integration, let you develop the most advanced XML and Web services apps for the environment of your choice.


SoapUI is Free and Open Source and is used for Inspecting Web Services, Invoking Web Services, Developing Web Services, Web Service Simulation and Web Service Mocking and Functional Testing of Web Services Load Testing of Web Services over HTTP.

Fiddler (HTTP)

Fiddler is a HTTP Debugging Proxy which logs all HTTP traffic between your computer and the Internet. Fiddler allows you to inspect all HTTP Traffic, set breakpoints, and “fiddle” with incoming or outgoing data. Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language.


SQLyog MySQL GUI is the most powerful MySQL manager and admin tool, combining the features of MySQL Query Browser, Administrator, phpMyAdmin and various other MySQL Front Ends and MySQL clients in a single intuitive interface.


Notepad++ is a free (as in “free speech” and also as in “free beer”) source code editor and Notepad replacement that supports several languages. It runs in the MS Windows environment.


Usefull for testing web application in different versions of IE in only one machine.


GIMP is the GNU Image Manipulation Program. It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

Tortoise SVN

TortoiseSVN is a really easy to use Revision control / version control / source control software for Windows. Since it’s not an integration for a specific IDE you can use it with whatever development tools you like. TortoiseSVN is free to use. You don’t need to get a loan or pay a full years salary to use it.

No more ‘LazyInitializationException’ in your integration tests

Below an example of what you need to do using JUnit 4 to prevent ‘LazyInitializationException’.

@ContextConfiguration(locations = { “classpath:application-context-test.xml” })
public class AccountServiceIntregrationTests extends
AbstractTransactionalJUnit4SpringContextTests {

protected static final String SESSION_FACTORY = “sessionFactory”;

protected static ApplicationContext ctx =
new ClassPathXmlApplicationContext(

protected static SessionFactory sessionFactory;

private AccountService accountService;

public static void oneTimeSetUp() throws Exception {
sessionFactory = (SessionFactory) ctx.getBean(SESSION_FACTORY);
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
new SessionHolder(session));

public static void oneTimeTearDown() throws Exception {
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager

public void testCreateAccount() {

// Do you test here.





Localization with Spring 2.5


In Spring 2.5 you can tell in your dispatcher servlet configuration to scan for @Controller annotations. That way you don’t configure URL mappings explicity in the dispacher servlet as you used to do before since the annotations are added to the controller classes themselves. It’s less XML and I really like it. However I did not know how to add the locale interceptor in this case, because  there was no urlMapping bean defined. I found out online you just need to add the following configuration to the dispatcher servlet.

<bean     class=”org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”>
<property name=”interceptors” ref=”localeChangeInterceptor” />


Hope this helps you too.