Showing posts from December, 2012

Bean definition inheritance

This artcle is taken from the book Getting started with Spring Framework

The following figure shows the dependencies of different application objects in an application:

The above figure shows that the PersonalBankingDao and FixedDepositDao classes are dependent on the DatabaseOperations class. If multiple beans in your application share a common set of configuration (properties, constructor arguments, and so on), you can create a bean definition that acts as a parent for other bean definitions.

The following example listing shows that the PersonalBankingDao and FixedDepositDao bean definitions make use of bean definition inheritance to specify that they are dependent on DatabaseOperations bean:

<bean id="databaseOperations" 
      class="sample.spring.chapter01.springbankapp.utils.DatabaseOperations" /> 

<bean id="daoTemplate" abstract="true"> 
        <property name="databaseOperations" ref="databaseOperations" /&…

Inner beans in Spring

This artcle is taken from the book Getting started with Spring Framework

If a dependency of a bean is not shared by multiple beans, you can consider defining the dependency as an inner bean. An inner bean is defined inside a <property> or <constructor-arg> element by using the <bean> element of Spring’s beans schema. You should note that an inner bean is only accessible to the bean definition enclosing it, and not to other beans registered with the Spring container.

The following example listing shows how we generally represent bean dependencies:

<bean id="service" class="sample.spring.chapter03.springbankapp.service.FixedDepositServiceImpl">
        <property name="fixedDepositDao" ref="dao" />

<bean id="dao" class="sample.spring.chapter03.springbankapp.dao.FixedDepositDaoImpl" />

The above example listing shows that the service bean is dependent on dao bean. If service bean is t…

Registering PropertyEditors with the Spring container

This article is taken from Getting started with Spring Framework book

To register property editors with the Spring container, you need to do the following:
1. Create a class that implements Spring’s PropertyEditorRegistrar interface. This class is responsible for registering property editors with the Spring container.
2. Configure the PropertyEditorRegistrar implementation as a Spring bean in the application context XML file.
3. Configure Spring’s CustomEditorConfigurer special bean in the application context XML file, and provide it with reference to the PropertyEditorRegistrar implementation (that you created in step 1 and configured in step 2).

The following configuration shows how CustomEditorConfigurer is configured:

<bean id=" myPropertyEditorRegistrar" class="sample.spring.chapter02.beans.MyPropertyEditorRegistrar " />

<bean id="editorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">

How a Spring BeanPostProcessor works

The following sequence diagram taken from Getting started with Spring Framework shows how Spring's BeanPostProcessor works:

The above diagram shows that when an instance of Spring container is created, MyBeanPostProcessor (a BeanPostProcessor implementation) is created before any regular Spring bean (like ABean) is created. When an ABean instance is created, it is passed to MyBeanPostProcessor's postProcessBeforeInitializationand postProcessAfterInitialization methods. Notice that MyBeanPostProcessor's postProcessBeforeInitialization is invoked before ABean's init method is invoked, and MyBeanPostProcessor's postProcessAfterInitialization is invoked after ABean's init method is invoked.  You can use postProcessBeforeInitializationand postProcessAfterInitialization methods to make modifications to the ABean instance.

Important links - Getting started with Spring Framework

'Getting started with Spring Framework' book that I started writing couple of months back with J Sharma is now available at amazon :

You can download the source code of the book from here:

You can post your questions and feedback here:!forum/getting-started-with-spring-framework

Detailed ToC of 'Getting started with Spring Framework'

Chapter 1 - Spring Framework basics
1-1 Introduction
1-2 Spring Framework modules
1-3 Why use Spring Framework?
Declarative transaction management
JMX (Java Management Extensions)
JMS (Java Message Service)
1-4 DI and Spring IoC container
Dependency Injection (DI)
Identifying application objects and their dependencies
Creating POJO classes corresponding to identified application objects
Creating the configuration metadata
Creating an instance of Spring IoC container
Access application objects from the Spring IoC container
1-5 Programming to interfaces
Scenario: Dependent class contains reference to the concrete class of dependency
Scenario: Dependent class contains reference to the interface implemented by dependency
Spring’s support for programming to interfaces design approach
1-6 Different approaches to instantiating Spring beans
Instantiating beans via static factory methods
Instantiating beans via instance factory methods
1-7 Dependency injection techniques
Setter-based DI