Spring AOP

Introduction

Aspects enable modularization such as transaction management that works across multiple types and objects.

Aspect: A modularization operation that cuts across multiple objects. Transaction management is a good example of a crosscutting concern in J2EE applications. In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (@AspectJ style).

  1. Point cut: Point cut is an expression. This expression identifies join points. Join points are basically methods defined in Service (or other) classes. Point cut is associated with Advice. Advice is applied on join-point methods which are identified by point-cut.

  2. Join Point: Join points are methods of Service (or other) classes. Methods are identified by point-cut expression. In Spring AOP, a join point is always represents a method execution.

  3. Advice: Advice is an Action taken by an aspect at a particular join point. There are different types of advises which include "around," "before" and "after" advice. An advice as an interceptor, maintaining a chain of interceptors "around" the join point.

Declarative Transaction Handling

A transaction is a serious of multiple database changes for a business operation that commit (save) together or in case of any exception rolled-back (cancel) together.

Business operations are executed by Service classes that in term call DAO classes to perform database operations. Since Service has business operations so Service classes are responsible for handling business transactions.

Spring allows a programmer to write transnational code, is called programmatic translation. Programmatic transactions are not recommended in modern applications. Spring allows another way called declarative transaction handling. Instead of writing codes, transactions can be declared for a Service class using XML configuration or using Annotations.

There are six transnational attributes declare at method level.

  1. Required (default)

  2. Required New

  3. Supported

  4. Not Supported

  5. Mandatory

  6. Never

All transactions are transparently handled by Spring AOP.

XML Configuration

You need to configure ORM transaction manager, transaction advice and Spring AOP pointcut to declare transaction attributes with Service class methods.

(1) tx:advice : tag associate methods with transactional attributes are REQUIRED, REQUIRED NEW, SUPPORTED, NOT SUPPORTED, NEVER and MENDATORY

<bean id="hibTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<tx:advice id="hibTxAdvice" transaction-manager="hibTransactionManager">

<tx:attributes>

<!-- all methods starting with 'find' and 'search' are read-only -->

<tx:method name="find*" read-only="true" />

<tx:method name="search*" read-only="true" />

<!-- other methods use the default transaction settings -->

<tx:method name="*" propagation="REQUIRED" />

</tx:attributes>

</tx:advice>

We inject Transaction Manager to Tx Advice. We can disable transaction by configuring method with read-only=true.

(2) aop:config : We provide an expression ( com.sunilos.service.*ServiceImpl.*(..) ) to the pointcut. Spring AOP will apply advice to the classes that satisfy expression of pointcut. .

<aop:config>

<aop:pointcut id="serviceOperations" expression="execution(* com.sunilos.service.*ServiceImpl.*(..))" />

<aop:advisor advice-ref="hibTxAdvice" pointcut-ref="serviceOperations" />

</aop:config>

Transactions will be applied on methods. If method will propagate unchecked exception then transaction will be rolled back by Spring AOP otherwise transaction will be committed.

Annotation Configuration

Annotation @Service and @Transnational will be used to declare transaction in a Service bean.

Following tags are used to enable annotation based transaction:

  1. context:component-scan: auto discover @Repository, @Service, @Component and @Controller spring beans

  2. tx:annotation-driven: Enables the configuration of transnational behavior based on annotations

Here is sample code to declare annotation based transaction:

@Service (value = "collegeService")

public class CollegeServiceImpl implements CollegeServiceInt {

@Autowired

private CollegeDAOInt dao = null; @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)

public College get(long id) {

return dao.findByPK(id);

} //..

}

Make following configurations in applicationContext.xml

<!-- Hibernate Transaction Manager -->

<bean id="hibTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<context:component-scan base-package="in.co.sunrays.annotation" />

<!-- enable the configuration of transnational behavior based on annotations -->

<tx:annotation-driven transaction-manager="hibTransactionManager" />


FAQ

Q: How do you handle transactions in your application?

Q: Which classes are handling transactions in your application?

Q: Are you applying manual transactions or declarative transactions?

Q: Which are the propagation attributes passed to @Transactional annotation?

A: Required, Required New, Supported, Not Supported, Mandatory, Never

Q: Which spring module do you use for transaction handling?

A: Spring AOP

Q: What is an Aspect?

Q: How do you create custom aspect?

A: Using @Aspect annotation

Q: Write the configuration code of Spring AOP?