Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
Most developers will acknowledge how implementing crosscutting concerns such as logging, auditing, security and transactionality can adversely affect business logic implementation. Such concerns "seem" to increase the complexity of existing business logic, at times making it difficult if not impossible to clearly distinguish business logic from the crosscutting concern implementation.
I had been hearing about aspect-oriented programming (AOP) being a solution for implementing crosscutting concerns. But having read through early AOP tools documentation, I was dissuaded from using AOP in my projects' recommendations, primarily because of a concern that average developers might not "get" AOP concepts, and the overall ability to debug applications, and maintain them, might be restricted to those few who understood AOP.
Recently released Spring 2.0 provides excellent, easy-to-use AOP capabilities. Spring AOP is now remarkably easy to use for the average Java developer with traditional object-oriented programming skills. This story shows how Spring 2.0 AOP can be effectively used to implement crosscutting concerns in a typical software project. We will consider the following crosscutting concerns for implementation:
Let us start with an example code snippet:
public String doSomething(String input)
{
//Logging
System.out.println("entering business method with:"+input);
//Security check for authorization of action (business-method)
//transactionality
try
{
//Start new session and transaction
//Some business logic
//Commit transaction
}
catch(Exception e)
{
//Rollback transaction
}
finally
{
//Close session
}
//Logging
System.out.println("exiting business method with:"+input);
return input;
}
As can be seen, the actual business logic in the business method appears hopelessly lost in the crosscutting concerns code for security, transactionality and logging. My intent is to demonstrate the ability to implement these concerns as cleanly segregated from the business logic as might be possible.
Before we begin, let's review a few basic definitions used in aspect-oriented programming and their implementation in Spring AOP.
execution(* com.myorg.springaop.examples.MyService*.*(..)). Asterisks in the expression refer to wildcards, as is conventional.
Let's try and implement each of the concerns using Spring 2.0 AOP.
Note: The example below assumes familiarity with basic Spring configurations. The entire Spring configuration file, as well as this story's full source code, can be downloaded from Resources.
Logging is the easiest of concerns to be implemented. All we need is a regular Java class that implements the logging concern as a Spring AOP aspect, as shown in this code:
package com.myorg.springaop.examples;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyLoggingAspect
{
public Object log(ProceedingJoinPoint call) throws Throwable
{
System.out.println("from logging aspect: entering method [" + call.toShortString()
+"] with param:"+call.getArgs()[0] );
Object point = call.proceed();
System.out.println("from logging aspect: exiting method [" + call.toShortString()
+ "with return as:" +point);
return point;
}
}
The above LoggingAspect class merely needs to be registered with Spring 2.0 as a regular Spring bean. Further, we need to register the LoggingAspect bean in the Spring configuration file as a Spring AOP aspect. This includes specifying the pointcut expression and the aspect's
advice type. The configuration for the pointcut, including the pointcut expression [execution(* com.myorg.springaop.examples.MyService*.*(..)) ], is shown in the listing below. The advice configuration, also shown below, specifies "around" as the advice type. Other
advice types, such as before and after, are also possible. The advice specification also includes the Java aspect class's
method to which the aspect will be applied. In this case it is the method log.
The code below is the Spring configuration for registering a logging aspect using Spring AOP:
<bean id="LoggingAspect" class = "com.myorg.springaop.examples.MyLoggingAspect"/>
<aop:config>
...
...
...
<aop:aspect ref="LoggingAspect">
<aop:pointcut id="myCutLogging"
expression="execution(* com.myorg.springaop.examples.MyService*.*(..))"/>
<aop:around pointcut-ref="myCutLogging" method="log"/>
</aop:aspect>
</aop:config>
Having implemented the above class and Spring configuration, effectively, we have instructed Spring AOP to execute the code
present in the MyLoggingAspect class's log( ) method dynamically around the business logic represented by the method doSomething( ).
To implement security, specifically authorization, we will use Acegi. Acegi is a popular and flexible security framework that can be used in enterprise applications. Acegi integrates well with Spring and uses Spring application contexts for all configurations. Using Acegi Security greatly simplifies implementing authorization in our application in a flexible manner.
| Subject | Replies |
Last post
|
|
By JavaWorld
|
11 |
05/16/08 06:28 AM
by Anonymous |
For an introduction to AOP, read "I Want My AOP!" Ramnivas Laddad (JavaWorld)
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq