Monday, December 31, 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" /> 

<bean id="fixedDepositDao" parent="daoTemplate" 
      class="sample.spring.chapter01.springbankapp.dao.FixedDepositDaoImpl" /> 

<bean id="personalBankingDao" parent="daoTemplate"
        class="sample.spring.chapter01.springbankapp.dao.PersonalBankingDaoImpl" />

In the above example listing, the daoTemplate bean definition defines the common configuration shared by both the fixedDepositDao and personalBankingDao bean definitions. As both the fixedDepositDao and personalBankingDao bean definitions require the databaseOperations dependency, the daoTemplate bean definition defines the databaseOperations dependency using the <property> element. The <bean> element’s parent attribute specifies the name of the bean definition from which the configuration is inherited. As the parent attribute value is daoTemplate for fixedDepositDao and personalBankingDao bean definitions, they inherit databaseOperations property from the daoTemplate bean definition.

Tuesday, December 25, 2012

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 the only bean that is dependent on the dao bean, then you can define the dao bean as an inner bean of service bean, as shown here:

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

In the above example listing, the bean definition for the FixedDepositDaoImpl class is inside the <property> element of service bean. If you compare the above example listing with the previous one, you’ll notice that the <property> element no longer specifies the ref attribute, and the <bean> element corresponding to FixedDepositDaoImpl class doesn’t have the id attribute anymore.

Sunday, December 23, 2012

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">
         <property name="propertyEditorRegistrars">
                 <ref bean="myPropertyEditorRegistrar"/>

For an example, refer to ch02-simple-types-examples project. 

Saturday, December 15, 2012

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 postProcessBeforeInitialization and 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 postProcessBeforeInitialization and 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
Constructor-based DI
1-8 Bean scopes
Choosing the right scope for your beans
1-9 Frameworks built on top of Spring
1.10 Summary

Chapter 2 - Configuring beans
2-1 Introduction
2-2 Bean definition inheritance
MyBank App – Bean definition inheritance example
What gets inherited ?
2-3 Constructor argument matching
Passing simple values and bean references using element
Constructor argument matching based on type
Constructor argument matching based on name
2-4 Configuring different types of bean properties and constructor arguments
Built-in property editors in Spring
Specifying values for different collection types
Specifying values for arrays
Default collection implementation for , and elements
2-5 Built-in property editors
2-6 Registering property editors with the Spring container
Creating a PropertyEditorRegistrar implementation
Configuring the CustomEditorConfigurer class
2-7 Concise bean definitions with p and c namespaces
2-8 Spring’s util schema
2-9 FactoryBean interface
MyBank App application – Storing events in the database
MyBank App – FactoryBean example
Accessing the FactoryBean instance
2-10 Summary

Chapter 3 - Dependency injection
3-1 Introduction
3-2 Inner beans
3-3 Explicitly controlling the bean initialization order with depends-on attribute
MyBank App – implied dependencies between beans
Implicit dependency problem
3-4 Singleton- and prototype-scoped bean’s dependencies
Singleton-scoped bean’s dependencies
Prototype-scoped bean’s dependencies
3-5 Obtaining new instances of prototype beans inside singleton beans
ApplicationContextAware interface
<lookup-method> element
<replaced-method> element
3-6 Autowiring dependencies
default / no
Making beans unavailable for autowiring
Autowiring limitations
3-7 Summary

Chapter 4 - Customizing beans and bean definitions
4-1 Introduction
4-2 Customizing bean’s initialization and destruction logic
Making Spring invoke cleanup method specified by the destory-method attribute
Cleanup methods and prototype-scoped beans
Specifying default bean initialization and destruction methods for all beans
InitializingBean and DisposableBean lifecycle interfaces
JSR 250’s @PostConstruct and @PreDestroy annotations
4-3 Interacting with newly created bean instances using BeanPostProcessor
BeanPostProcessor example – Validating bean instances
BeanPostProcessor example – Resolving bean dependencies
BeanPostProcessor behavior for FactoryBeans
4-4 Modifying bean definitions using BeanFactoryPostProcessor
BeanFactoryPostProcessor example
4-5 Summary

Chapter 5- Annotation-driven development with Spring
5-1 Introduction
5-2 Identifying Spring components with @Component
5-3 @Autowired - autowiring dependencies by type
5-4 @Qualifier – autowiring dependencies by name
5-5 JSR 330’s @Inject and @Named annotations
5-6 JSR 250’s @Resource annotation
5-7 @Scope, @Lazy, @DependsOn and @Primary annotations
5-8 Simplifying component configuration using @Value annotation
5-9 Validating objects using Spring’s Validator interface
5-10 Specifying constraints using JSR 303 annotations
JSR 303 support in Spring
5-11 Programmatically configuring Spring beans using @Configuration and @Bean annotations
5-12 Summary

Chapter 6 - Database interaction using Spring
6-1 Introduction
6-2 MyBank App application’s requirements
6-3 Developing the MyBank App application using Spring’s JDBC module
Configuring a data source
Creating DAOs that use Spring’s JDBC module classes
6-4 Developing the MyBank App application using Hibernate
Configuring SessionFactory instance
Creating DAOs that use Hibernate API for database interaction
6-5 Transaction management using Spring
MyBank App’s transaction management requirements
Programmatic transaction management
Declarative transaction management
Spring’s support for JTA
6-6 Summary

Chapter 7 - Messaging, emailing, asynchronous method execution, and caching using Spring
7-1 Introduction
7-2 MyBank App application’s requirements
7-3 Sending JMS messages
Configuring ActiveMQ broker to run in embedded mode
Configuring a JMS ConnectionFactory
Sending JMS messages using JmsTemplate
Sending JMS messages within a transaction
Dynamic JMS destinations and JmsTemplate configuration
JmsTemplate and message conversion
7-4 Receiving JMS messages
Synchronously receiving JMS messages using JmsTemplate
Asynchronously receiving JMS messages using message listener containers
7-5 Sending emails
7-6 Task scheduling and asynchronous execution
TaskExecutor interface
TaskScheduler interface
@Async and @Scheduled annotations
7-7 Caching
Configuring a CacheManager
Caching annotations - @Cacheable, @CacheEvict and @CachePut
7-8 Running the MyBank App application
7-9 Summary

Chapter 8 - Aspect-oriented programming
8-1 Introduction
8-2 A simple AOP example
8-3 Spring AOP framework
Proxy creation
expose-proxy attribute
8-4 Pointcut expressions
@Pointcut annotation
execution and args pointcut designators
bean pointcut designator
Annotations-based pointcut designators
8-5 Advice types
Before advice
After returning advice
After throwing advice
After advice
Around advice
8-6 Spring AOP - XML schema-style
Configuring an AOP aspect
Configuring an advice
Associating a pointcut expression with an advice
8-7 Summary