Saturday, December 15, 2012

Wednesday, December 12, 2012

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

Saturday, January 14, 2012

Spring Roo 1.1 Cookbook review by Shekhar Gulati

Many thanks to Shekhar for reviewing Spring Roo 1.1 Cookbook :)

You can visit Shekhar's blog and read the complete review here :

View and add dynamic finder methods

A dynamic finder method is a finder method for which you don't need to write a JPA query.

Download ch03_persistent_entities.roo script from the source code that accompanies Spring Roo 1.1 Cookbook:

The script sets up Hibernate as a persistence provider and creates a Flight entity, which
has FlightKey as its composite primary key class. Additionally, the script adds fields to the
Flight and FlightKey classes. If you are using a different database than MySQL or your
connection settings are different than what is specified in the script, then modify the script

To view dynamic finder methods, follow the given steps:

1. Start Roo shell and set the focus of the subsequent commands on the Flight entity using the focus
roo> focus --class ~.domain.Flight

2. Execute the finder list command to view the list of candidate dynamic finder
methods for the Flight entity, as shown here:
~.domain.Flight roo> finder list
findFlightsByCreatedDateBetween(Date minCreatedDate, Date
findFlightsByCreatedDateGreaterThan(Date createdDate)

To add dynamic finder methods, follow the given steps:

1. Add the findFlightsByDestinationLikeAndOriginLike dynamic finder
method to the Flight entity using the finder add command:

.. roo> finder add findFlightsByDestinationLikeAndOriginLike
Updated SRC_MAIN_JAVA\sample\roo\flightapp\domain\
Created SRC_MAIN_JAVA\sample\roo\flightapp\domain\Flight_Roo_Finder.aj

The following code shows the auto-generated implementation of the
findFlightsByDestinationLikeAndOriginLike finder method in the Flight_Roo_Finder.aj file:

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
public static TypedQuery
Flight.findFlightsByDestinationLikeAndOriginLike(String destination, String origin) {
  if (destination == null || destination.length() == 0)
     throw new IllegalArgumentException("The destination argument is required");
  if (origin == null || origin.length() == 0)
     throw new IllegalArgumentException("The origin argument is required");
  EntityManager em = Flight.entityManager();
  TypedQuery q = em.createQuery("SELECT Flight FROM Flight AS flight WHERE            
                    LOWER(flight.destination) LIKE
                    LOWER(:destination) AND LOWER(flight.origin) LIKE
                    LOWER(:origin)", Flight.class);
   q.setParameter("destination", destination);
   q.setParameter("origin", origin);
   return q;

Monday, December 12, 2011

Adding JSON support to domain objects and controllers

This post shows how you can add JSON support to your applications using Spring Roo. The information in this post has been taken from Spring Roo 1.1 Cookbook.

Step 1: Download Roo scripts and sample code from the following location:

Step 2: Execute the ch04_web-app.roo script that creates the flight-app Roo project, sets up
Hibernate as the persistence provider, configures MySQL as the database for the application,
creates the Flight and FlightDescription JPA entities, and defines a many-to-one
relationship between the Flight and FlightDescription entities. If you are using a
different database than MySQL or your connection settings are different than what is specified
in the script, then modify the script accordingly.

Step 3: Execute the controller all command to create controllers and JSPX views corresponding
to JPA entities in the flight-app project, as shown here:
.. roo> controller all --package ~.web
Execute the perform eclipse command to update the project's classpath settings, as
shown here:
.. roo> perform eclipse
Now, import the flight-app project into your Eclipse IDE.

Step 4: To add the json support execute the json add command against the Flight JPA entity:
~.domain.Flight roo> json add --class ~.domain.Flight
Updated SRC_MAIN_JAVA\...\domain\
Created SRC_MAIN_JAVA\...\domain\Flight_Roo_Json.aj
Created SRC_MAIN_JAVA\...\web\FlightController_Roo_Controller_Json.aj

Executing the json add command creates
a *_Roo_Json.aj AspectJ ITD, which defines methods for converting objects of the class to
JSON documents and vice versa, as shown here:

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

privileged aspect Flight_Roo_Json {
  public String Flight.toJson() {
    return new JSONSerializer().exclude("*.class").serialize(this);
  public static Flight Flight.fromJsonToFlight(String json) {
    return new JSONDeserializer().use(null, Flight.class).deserialize(json);
  public static String Flight.toJsonArray(
    Collection collection) {
  public static Collection
    Flight.fromJsonArrayToFlights(String json) {

The following code shows the FlightController_Roo_Controller_Json.aj ITD, which
was generated:
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ResponseBody;

privileged aspect FlightController_Roo_Controller_Json {

   @RequestMapping(value = "/{flightId}", method = RequestMethod.GET, 
     headers =  "Accept=application/json")
  public Object FlightController.showJson(@PathVariable("flightId") Long flightId) {
    Flight flight = Flight.findFlight(flightId);
    if (flight == null) {
         HttpHeaders headers= new HttpHeaders();
         headers.add("Content-Type", "application/text");
         return new ResponseEntity(headers, HttpStatus.NOT_FOUND);
    return flight.toJson();

Monday, November 28, 2011

Spring Roo 1.1 Cookbook review by Cengiz ├ľner

I just came across review of Spring Roo 1.1 Cookbook by . Many thanks to Cengiz for writing the review :)

You can visit Cengiz's blog and read the complete review here :