Tuesday 31 December 2013

Spring ReloadableResourceBundleMessageSource example

In Spring, you can use ReloadableResourceBundleMessageSource to resolve text messages from properties file, base on the selected locales. See following example :

In contrast to ResourceBundleMessageSource, this class supports reloading of properties files through the "cacheSeconds" setting, and also through programmatically clearing the properties cache. Since application servers do typically cache all files loaded from the classpath, it is necessary to store resources somewhere else (for example, in the "WEB-INF" directory of a web app). Otherwise changes of files in the classpath are not reflected in the application.


Tuesday 24 December 2013

Spring Collections

You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
Now what about if you want to pass plural values like Java Collection types List, Set, Map, and Properties. To handle the situation, Spring offers four types of collection configuration elements which are as follows:

Monday 23 December 2013

PropertyPlaceholderConfigurer

A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.

Friday 20 December 2013

Load Multiple Spring Configuration Files

Problem

In a large project structure, the Spring’s bean configuration files are located in different folders for easy maintainability and modular. For example, Spring-Common.xml in common folder, Spring-Connection.xml in connection folder, Spring-ModuleA.xml in ModuleA folder…and etc.

You may load multiple Spring bean configuration files in the code :

Spring DI via constructor

A simple Spring example to show you how to dependency inject a bean via Constructor method

WildAnimal.java – Interface backbone using which dependency injection is done.

Spring DI via setter method

A simple Spring example to show you how to dependency inject a bean via setter method, the most common used DI method.

WildAnimal.java – Interface backbone using which dependency injection is done.

Dependency Injection

Dependency Injection takes the level of decoupling that began with the Dependency Inversion Principle one step further.

Dependency injection has the concept of an assembler or what in Java is commonly referred to as a Factory -- that instantiates the objects required by an application and “injects” them into their dependent objects.

Spring Architecture

Spring is well-organized architecture consisting  of seven modules. Modules in the Spring framework are:


Spring IOC Container

The IoC container is responsible to instantiate, configure and assemble the objects. The IoC container gets informations from the XML file and works accordingly. The main tasks performed by IoC container are:
  • to instantiate the application class
  • to configure the object
  • to assemble the dependencies between the objects
There are two types of IoC containers. They are:
  1. BeanFactory
  2. ApplicationContext

Spring bean scopes

Spring bean scope is used to decide which type of bean instance should be return from Spring container.

5 types of bean scopes supported :

  1. singleton – Return a single bean instance per Spring IoC container
  2. prototype – Return a new bean instance each time when requested
  3. request – Return a single bean instance per HTTP request. *
  4. session – Return a single bean instance per HTTP session. *
  5. globalSession – Return a single bean instance per global HTTP session. *
In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.

Maven + Spring hello world example

Below are the Steps to Create Sample Hello World Example in Spring

Add the Spring 3.0 dependencies listed below in Maven’s pom.xml file.