| home / programming / spring / 2 | [previous] |
|
|
Enterprise applications often require certain services such as security and transactional support. One way of applying these services is to code support for them directly into the classes that use them. For example, to handle transactions, you may place the following snippet throughout your code:

The problem with handling transactions this way is that you may repeat the same transaction handling code several times—once for each time you need a transactional context. What’s more, your application code is responsible for more than its core functionality.
EJB simplifies things by making it possible to declare these services and their policies in the EJB deployment descriptor. With EJB it is possible to write components that are ignorant of the fact that they are in a transactional context or being secured and then declare the transactional and security policies for those components in the EJB deployment descriptor. For example, to ensure that a method is transactional in EJB, you simply place the following in the deployment descriptor:

EJB has hung its hat on how it simplifies infrastructure logic such as transactions and security. But as we discussed in the introduction to this chapter, EJB has complicated matters in other ways.
Although Spring’s AOP support can be used to separate cross-cutting concerns from your application’s core logic, its primary job is as the basis for Spring’s support for declarative transactions. Spring comes with several aspects that make it possible to declare transaction policies for JavaBeans. And the Acegi Security System (another open-source project associated with Spring) provides declarative security to JavaBeans. As with all Spring configuration, the transactional and security policies are prescribed in a Spring configuration file.
For example, suppose that instead of a knight your application handles student
registration for training courses. Perhaps you have a bean called StudentServiceImpl that implements the following interface:
public StudentService {
public void registerForCourse(Student student, Course course);
}
This bean may be registered in the Spring bean XML file as follows:

StudentService’s registerForCourse() method should perform the
following actions:
1 Verify that there is an available seat in the course.
2 Add the student to the course’s roster.
3 Decrement the course’s available seat count by 1.
4 Notify the student by e-mail of a successful registration.
All of these actions should happen atomically. If anything goes bad, then all
should be rolled back as if nothing happened. Now imagine if instead of a minstrel
providing musical logging to this class, you were to apply one of Spring’s
transaction manager aspects. Applying transactional support to StudentServiceImpl might be as simple as adding the lines shown in listing 1.13 to the bean
XML file.

Here we make use of Spring’s TransactionProxyFactoryBean. This is a convenience
proxy class that allows us to intercept method calls to an existing class and
apply a transaction context. In this case we are creating a proxy to our StudentServiceImpl class and applying a transaction to the registerForCourse() method.
We are also using HibernateTransactionManager, the implementation of a transaction
manager you would most likely use if your application’s persistence layer is
based on Hibernate.
Although this example leaves a lot to be explained, it should give you a glimpse of how Spring’s AOP support can provide plain-vanilla JavaBeans with declarative services such as transactions and security. We’ll dive into more details of Spring’s declarative transaction support in chapter 5.
Next week, in our final week of this series, we look at Spring Alternatives.
Spring in Action: A Spring Jump Start is written by Craig Walls and Ryan Breidenbach and reproduced from "Spring in Action" by permission of Manning Publications Co. ISBN 1932394354, copyright 2005. All rights reserved. See http://www.manning.com for more information.| home / programming / spring / 2 | [previous] |
Created: March 27, 2003
Revised: March 17, 2005
URL: http://webreference.com/programing/spring/2