I am the lead of JBoss AOP and also work on the Microcontainer http://kabir-khan.blogspot.com/ Kabir has posted 2 posts at DZone. View Full User Profile

An Introduction to Aspect-Oriented programming with JBoss AOP

08.28.2008
| 71225 views |
  • submit to reddit

JBoss Application Server ships with support for aspect-oriented programming, so you can use AOP in your applications deployed in JBoss AS. JBoss AS 5, which is currently available as a community release, has AOP built into its core. However, JBoss AOP is also available as a standalone framework for use in your other applications.

This article will take a simple example, and use JBoss AOP to add to its behaviour, while explaining some of the core functionality of JBoss AOP. We will look at encapsulating cross-cutting concerns into aspects, have a look at around advices vs the new lightweight advices in the upcoming JBoss AOP 2.0 release, and also look at using interface-introductions and mixins to add interfaces to your classes.

Table of Contents

 

Our Core Application

The application we will look at is is a simple banking application. It has a BankAccount class:
package bank;
public class BankAccount
{
int accountNumber;
int balance;

public BankAccount(int accountNumber)
{
System.out.println("*** Bank Account constructor");
this.accountNumber = accountNumber;
}

public int getAccountNumber()
{
return accountNumber;
}

public int getBalance()
{
return balance;
}

public void debit(int amount)
{
System.out.println("*** BankAccount.debit()");
balance -= amount;
}

public void credit(int amount)
{
System.out.println("*** BankAccount.credit()");
balance += amount;
}
}

In addition it has a main Bank class:

package bank;

import java.util.HashMap;
import java.util.Map;

public class Bank
{
static Map bankAccounts = new HashMap();

public static void transfer(BankAccount from, BankAccount to, int amount)
{
from.debit(amount);
to.credit(amount);
}

public static void main(String[] args)
{
System.out.println("*** Creating account 1");
BankAccount acc1 = new BankAccount(1);
acc1.credit(150);
bankAccounts.put(acc1.getAccountNumber(), acc1);

System.out.println("*** Creating account 2");
BankAccount acc2 = new BankAccount(2);
acc2.credit(230);
bankAccounts.put(acc2.getAccountNumber(), acc2);

System.out.println("*** Balance acount 1: " + acc1.getBalance());
System.out.println("*** Balance acount 2: " + acc2.getBalance());

//Transfer some money
System.out.println("*** Transfer 50 from account 1 to account 2");
transfer(acc1, acc2, 50);

System.out.println("*** Balance acount 1: " + acc1.getBalance());
System.out.println("*** Balance acount 2: " + acc2.getBalance());
}
}

As you can see, we create two bank accounts with their account numbers, set the initial balances, and then transfer 50 from account1 to account 2. Running this simple example we get the following expected output:

*** Creating account 1
*** Bank Account constructor
*** BankAccount.credit()
*** Creating account 2
*** Bank Account constructor
*** BankAccount.credit()
*** Balance acount 1: 150
*** Balance acount 2: 230
*** Transfer 50 from account 1 to account 2
*** BankAccount.debit()
*** BankAccount.credit()
*** Balance acount 1: 100
*** Balance acount 2: 280

The code for this example can be found in the listing1/ folder of the download bundle.

AttachmentSize
jboss-aop-samples.zip4.76 MB
Published at DZone with permission of its author, Kabir Khan.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Joshua Partogi replied on Fri, 2008/08/29 - 10:05pm

This is certainly nice, but can JBoss AOP configured via annotation instead of xml?

Jaikiran Pai replied on Sat, 2008/08/30 - 3:52am

Good article, Kabir.

I have a question. Is the aspect code allowed to change the value that is being passed to the  joinpoint? I mean, if the credi(int amount) method was being passed a value of 200, can the code in the aspect change this value to 100 before the credit method is invoked? If yes, is there any way to secure such access?

 

 

Kabir Khan replied on Mon, 2008/09/01 - 5:06am

Joshua,

Yes annotations can be used instead.An example can be found here and the containing directory contains more examples. If using annotations, instead of passing in the path of a jboss-aop.xml file with -Djboss.aop.path, you point JBoss AOP to a directory containing your annotated aspects using -Djboss.aop.class.path. The tutorial examples that come with the download will show how in more detail.

Kabir Khan replied on Mon, 2008/09/01 - 5:13am

Jaikiran,

The aspect code can modify the values that are passed in. There is currently no way to stop this, if that is what you mean by securing it. If this feature is important to you please ask for it on our user forums, and we will take it into consideration. 

sowjanya gutta replied on Tue, 2009/02/10 - 11:37pm

I was unable to get the code from this(jboss-aop-samples.zip).

sowjanya gutta replied on Wed, 2009/02/11 - 1:18am

can any one of you please update the code samples.

Andy Surfer replied on Sun, 2011/02/13 - 9:16pm

good article, thank you for providing examples. There are lots of Jboss books here (toward the bottom of the page)

Al3ab Co replied on Mon, 2011/09/05 - 12:55am

Hello So great article, this is exactly what I needed today, keep on good work, I have a question wether if JBoss can work in international languages. Thanks for your time and effort again. Greetings, Mike Soft. Engineer Web: al3ab

Steve Dean replied on Tue, 2011/10/11 - 3:06am

JAVA, great application. Just getting to grips with it really. I am reading about Aspect Orientated Programming, it really does seem the way to go as far as business is concerned. I am trying to compile a few programs for our Tipper Hire business. Any advice would be welcome.

replied on Sun, 2011/11/27 - 12:11pm

Thanks A lot java is great! İlahi Dinle

Huber Grant replied on Wed, 2011/11/30 - 8:26am

Good to know about this. The car accident injury claim is a sound option.

Tom Smith replied on Fri, 2011/12/16 - 7:20pm

 Visit the <a href="http://fishkeeping-forum.com">fishkeeping forum</a>

Tom Smith replied on Fri, 2011/12/16 - 7:22pm

visit the http://usedcarforum.co.uk

Tom Smith replied on Fri, 2011/12/16 - 7:22pm in response to: Al3ab Co

<a href="http://fishkeeping-forum.com">fishkeeping forum</a>

shaunM (not verified) replied on Fri, 2011/12/23 - 8:53am

I am a tourist. My hobby is spend holidays in summer.very nice pools. i never watch before. every person want to take rest in summer . he use pools for relaxing.thanks for share information.

 

 

http://www.webinsect.com/
 

Richard Driver replied on Sat, 2011/12/31 - 7:09pm

Very Informative!

 Your core application for banking is much appreciated!

Online Business - Richard Driver

Kimo2012 Medo replied on Sat, 2012/01/07 - 8:46am

thank you

<a title="افلام" href="http://www.fbrka.com">افلام</a>
<a title="افلام عربى" href="http://www.fbrka.com/cat-20,start-0">افلام عربى</a>
<a title="افلام اجنبى" href="http://www.fbrka.com/cat-21,start-0">افلام اجنبى</a>

Steve Newton replied on Sun, 2012/01/08 - 4:43am

I am reading about Aspect Orientated Programming, it really does seem the way to go as far as business is concerned but I need to learn more. fromBangkok Cheap Hotels Gold Coast Cheap Hotels Pattaya Hotels Cheap Australia Cheap Holidays

Jimmy Jhon replied on Thu, 2012/01/26 - 3:12am

If using annotations, instead of passing in the path of a jboss-aop.xml file with -Djboss.aop.path, you point JBoss AOP to a directory containing your annotated aspects using -Djboss.aop.class.path. The tutorial examples that come with the download will show how in more detail. business setup in dubai

Tom Smith replied on Sat, 2012/01/28 - 9:05am

<a href="http://usedcarforum.co.uk/forum.php">Uased Car Forum</a>

Tom Smith replied on Sat, 2012/01/28 - 9:06am

<a title="http://en.wikipedia.org/wiki/Main_Page">Wikipedia</a>

Tom Smith replied on Sat, 2012/01/28 - 9:07am

http://usedcarforum.co.uk/forum.php

Tom Smith replied on Sat, 2012/01/28 - 9:11am

<a href="http://usedcarforum.co.uk/forum.php">used car forum</a>

Tom Smith replied on Sat, 2012/01/28 - 9:13am

<a href="http://usedcarforum.co.uk/forum.php">car forum</a>

Jimmy Jhon replied on Wed, 2012/02/01 - 1:22am

Interesting post and thanks for sharing. Some things in here I have not thought about before.Thanks for making such a cool post which is really very well written.will be referring a lot of friends about this.Keep blogging. how to talk dirty

Jimmy Jhon replied on Wed, 2012/02/01 - 10:27am

Hello So great article, this is exactly what I needed today, keep on good work, I have a question wether if JBoss can work in international languages. Thanks for your time and effort again. Greetings, Mike Soft. Engineer Web how to talk dirty

Jimmy Jhon replied on Thu, 2012/02/02 - 2:43am

Hey its been really a very good and informative post to read on. I will keep these points in my mind from now onwards, hope will not commit mistakes. hochwertige Nahrungsergänzung

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.