Spring Book – Chapter 14 – Spring MVC and Spring Web Flow

@ExceptionHandler

It is an annotation which can be used in controller methods for having a tighter control of exception handling within a single controller. Methods which are annotated with this annotation will be called when the controller methods throws appropriate exceptions, but it will only applicable to this declare controller. Listing 14-18 shows usage of ExceptionHandler annotation in a typical controller class.

Listing 14-18. Usage of ExceptionHandler annotation in controller class

As you can see in Listing 14-1 above, the method signature can be flexible and you have provision to use conventions.

Message Source

Spring resolves all messages which are parameterized and internationalized using MessageSource interface as shown in Listing 14-19 below.

Listing 14-19. MessageSource interface definition

The messages are loaded from properties file in which they are represented in dot natation as shown below:

While using Spring’s default MessageSource, the property files are loaded according to the user’s Locale. It looks for locale specific properties file (mypage_es.properties – Spanish Locale) initially and then it looks the English version properties file (mypage_en.properties) and as a fall back look into property file which doesn’t specify any Locale (mypage.properties).

For most part of your application to work, using default MessageSource implementations provided by Spring Framework should suffice. But, if you would like to pull and resolve the messages from different source, you can write your own MessageSource implementation doing what is required for your application.

There are various built-in MessageSource implemenations provided by the Spring Framework. Some of which are given below:

  • springframework.context.support.ResourceBundleMessageSource – It uses underlying JDK’s ResourceBundle implementation in association with standard message parsing provided by MessageFormat to accesses resource bundles using specified base names for resolving the messages.
  • springframework.context.support.ReloadableResourceBundleMessageSource – similar to ResourceBundleMessageSource but in contrast supports reloading of properties file through the “cacheSeconds” setting and also through programmatically clearing the properties cache from the application end. This is considered to be faster compared to ResourceBundleMessageSource which might be a bit of surprise looking at the implementation details.
  • springframework.context.support.StaticMessageSource – simplest MessageSource implementation which allows messages to be registered programmatically. Supports basic internationalization and are basically used for testing purposes.

Page Visitors: 8014

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

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