spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / spring / 2 To page 1To page 2To page 3current page
[previous]

Technical Lead
Thomson Reuters (Markets) LLC
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


Spring in Action: A Spring Jump Start. Part 2

1.5.3 AOP in the enterprise

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:

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 To page 1To page 2To page 3current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

Created: March 27, 2003
Revised: March 17, 2005

URL: http://webreference.com/programing/spring/2