-J2EE Developer at Turkiye Is Bankası Core Banking Exchange project -Java-Flex Developer in TAV IT. Built softwares for several international airport (istanbul, ankara, izmir, tblisi, batumi, emfidha) with JPA/Spring/CXF/BlazeDS on server side and Flex on rich clients. -JavaEE&Process Project Laader at HSBC. -Presenter at Eclipsist 2008, on Rapid Rich Internet Application Development. Murat has posted 8 posts at DZone. View Full User Profile

Aspect Oriented Programming - A Different Aspect.

07.24.2008
| 6425 views |
  • submit to reddit

Yesterday I was having a meeting about a new project startup. At some level the meeting was stuck on an argument of logging. Well I suggested the same thing as any experienced developer would suggest... "Why don't we use the Aspects?".

Our project is running on Websphere Application Server and using JDK 1.4 which means we won't be using EJB3 or annotations. So I just tried AspectJ - although I found the examples were confusing, what I achieved was very simple and easy. Here is a basic tutorial on how to enable and using Aspects.

Let's say we have a web application running on any server consisting of several servlets and we want to know the count of servlet calls and we also want to log the parameters received. If we are in the beginning of the project we can just copy several lines of codes to all doGet or doPost methods. Seems easy but what if the project is at the end we need to add that feature and what if the required parameters are subject to change. In this case everytime we need to visit every single doGet method and change everything again.

Aspects are interceptors - you may just imagine registering an invisible listener to the methods you want to watch and Aspects just do whatever you need. What makes them great for logging is that you just register (or give a naming pattern) of the methods and from there the Aspects will watch execution or exiting of your methods. So lets code..

This our test servlet, so just imagine we have hundreds of those with different parameters and codes.

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AspectTest extends HttpServlet implements Servlet {

public AspectTest() {
super();
}

public void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
System.out.println("*******************servlet running");
}
}


Now lets enable AspectJ, I assume you are using Eclipse and already installed AspectJ plugin. If not just use the Eclipse update site.

Right click your project and click to Convert to AspectJ Project. Now we have added AspectJ support. Next click select New > Aspect. We can code our aspect now.

public aspect AutoLog {
pointcut loggableCalls() : execution(public * *.doGet(..));

before() : loggableCalls(){
System.out.println("***getting in "+ thisJoinPoint.getSignature().toString());
}

after() : loggableCalls(){
System.out.println("***getting out "+ thisJoinPoint.getSignature().toString());
}
}


So simply, the pointcut just captures all doGet methods of all classes in all packages and then before and after methods just prints the class and method name. We can now deploy our project on server and test our servlet.
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O ***getting in void AspectTest.doGet(HttpServletRequest, HttpServletResponse)
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O *******************servlet running
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O ***getting out void AspectTest.doGet(HttpServletRequest, HttpServletResponse)


Now lets get more advanced and log all parameters going into our servlet. We only need to change our before block.

before() : loggableCalls(){
System.out.println("***getting in "+ thisJoinPoint.getSignature().toString());

Object[] obj = thisJoinPoint.getArgs();
HttpServletRequest request=(HttpServletRequest)obj[0];
Enumeration enumeration=request.getParameterNames();
while (enumeration.hasMoreElements()){
String param=enumeration.nextElement().toString();
System.out.println(param+": "+request.getParameter(param));
}
}


After we publish, lets run the servlet with some parameters...
https://localhost:9444/AspectWEB/AspectTest?a=aa&b=bb&c=mneokjnfvjr&d=nnnnnıerhvnj

And the output is..

[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O ***getting in void AspectTest.doGet(HttpServletRequest, HttpServletResponse)
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O b: bb
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O a: aa
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O d: nnnnnierhvnj
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O c: mneokjnfvjr
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O *******************servlet running
[7/18/08 10:18:59:162 EEST] 0000005f SystemOut O ***getting out void AspectTest.doGet(HttpServletRequest, HttpServletResponse)



Very simple and very efficient, don't worry about the future requests of your boss or customer. All you need to change is either the before or after block. If this is not agile can please someone tell me what is?
References
Published at DZone with permission of its author, Murat Yener. (source)

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

Comments

Sumit Kishore replied on Thu, 2008/07/24 - 11:05am

And what was different about this aspect? That you're using AOP at all?

William Louth replied on Thu, 2008/07/24 - 3:43pm

For what you are doing you might as well use a ServletFilter. 

Does the world need another AOP solution that uses logging? NO, the 1 million other implementations are more than enough even though to date I have yet to see an actual good implementation published.

AOP promised to reduce so much code clutter but in the end it only replicated the clutter on a much grander scale.

William 

Murat Yener replied on Thu, 2008/07/24 - 5:10pm in response to: Sumit Kishore

The aspect is.... to post a simple example with aop. Using or not is all your desicion

Murat Yener replied on Thu, 2008/07/24 - 5:20pm in response to: William Louth

Yes I could use similar initializers if i used EJB, or any similar thing if any other technology. I wonder what is wrong to see aspect way instead of the servletfilter way.

AOP is nice but i dont believe you can just depand everything on it. AOP is just like garlic, if you use some it is nice, to much just stinks.. and if you just use it for vampires than you are just dreaming...

Tihomir Surdilovic replied on Thu, 2008/07/24 - 8:08pm

I think promoting AspectJ for any development efforts, no matter how small, is a good thing. However, I find the provide example very incomplete and many details are hidden by the statement that you are using Eclipse with the AspectJ plugin. How do I use this aspect outside of Eclipse? Do you explain the core concepts of AspectJ - like what is a joinpoint, a pointcut, advice and what is a crosscutting concern??? Do you explain that you are using compile-time weaving of your aspect into your code and that it means having to compile your code with the AspectJ ajc compiler? Do you explain that it is also possible to load-time weave your aspect into the code and what the tradeoffs are? The reason to use AOP is to provide robust reusable implementations for crosscutting concerts. Logging is one of these concerns of course and used by so many articles as the simplest example of how to implement AOP. The logging solution shown here is somewhat misleading that the pointcut expression covers only a small part of what logging in an application really is. 

Since you mention this is work-related, I think next time it would be better to first complete your solution before posting your code to read and use :)

Overall, I applaud the attempt to promote AspectJ.    

Murat Yener replied on Fri, 2008/07/25 - 1:15am

As I mentioned in the post, I was not aiming to publish a complete reference since there have been so many nice complete documentation and I don't believe I can prepare a better one. What i tried to do was just a very simple example to people who dont know much about Aspects and to let them prepare a running example.

zhou lifeng replied on Fri, 2008/07/25 - 1:05am

i don't understand

William Louth replied on Fri, 2008/07/25 - 1:19am

What i tried to do was just a very simple example to people who dont know much about Aspects and to let them prepare a running example.

And at the same time shoot themselves in the foot if applied similarly to their own code base because this little example has many potential issues with it (hint: re-entrant). If you are really going to use aspectj for such logging concerns you might as well fire up a debugger it would be much more intuitive and easier to use and most importantly safe.

You would not believe the number of times I have come across performance issues because someone has deployed such an logging example to actually monitor performance or find a bug which magically disappears when such aspects are installed. 

William 

Murat Yener replied on Fri, 2008/07/25 - 1:31am in response to: William Louth

Well William debugging is a very very known drawback of Aspects.. but I used to hear same performance issues for xp and now hearing for vista, i hear complaints about how hard to use linux too. Everything has a side to complain. I am not writing a reference book on AspectJ, and everyone should judge their own preference of technology. I just want to make it easier to try and judge for them. And not everyone has evaluated or aware of aspects so why dont we just give them a try .)

William Louth replied on Fri, 2008/07/25 - 1:40am in response to: Murat Yener

I have nothing against AspectJ I have written more custom aspects than most developers I know. AOP is extremely powerful technology but one must invest time and effort to clearly understand the concepts and the implications of particular design approaches and implementations. Some of this is covered in the AspectJ user guide which includes a simple tracing aspect which is explained much better here. Maybe you could have just referenced this chapter in the aspect docs and then showed how you extended the simple tracing aspect for you particular needs. 

With AOP it is important you know what you are doing. With power comes responsibility. The current state of AOP runtimes and programming interfaces lends this technology more suitable to a system developer technology.

William Louth replied on Fri, 2008/07/25 - 1:42am

Very simple and very efficient, don't worry about the future requests of your boss or customer. All you need to change is either the before or after block. If this is not agile can please someone tell me what is?

This is an extraordinary statement considering the code listed and the lack of information provided. There is nothing efficient about the code and I would be very very worried if such code would even be considered for a production environment. I fail to see what Agile has got to do with this.

Stas Ostapenko replied on Fri, 2008/07/25 - 3:11am

You didn't mention nothing about aspect weaving. It is very important stuff.

J Szy replied on Fri, 2008/07/25 - 4:06am in response to: William Louth

[quote=wl88194]

For what you are doing you might as well use a ServletFilter.

[/quote]

Interestingly enough, most examples of AOP illustrate logging or logging and sometimes also logging. I might even start to think that it's the only non-toy use of AOP. :-)

Stas Ostapenko replied on Fri, 2008/07/25 - 4:14am in response to: J Szy

[quote=partyzant]

Interestingly enough, most examples of AOP illustrate logging or logging and sometimes also logging. I might even start to think that it's the only non-toy use of AOP. :-)

[/quote]

 I had the same feeling :) I wrote a blog entry about another one AOP usecase :

Search with Spring Hibernate Lucene and Aspect Oriented Programming in action

Matthew Harrison replied on Fri, 2008/07/25 - 5:17am in response to: Sumit Kishore

"...what's so different...?"

It's a play on words...

Matthew Harrison replied on Fri, 2008/07/25 - 5:23am in response to: Stas Ostapenko

[quote=daoway][quote=partyzant]

Interestingly enough, most examples of AOP illustrate logging or logging and sometimes also logging. I might even start to think that it's the only non-toy use of AOP. :-)

[/quote]

 I had the same feeling :) I wrote a blog entry about another one AOP usecase :

Search with Spring Hibernate Lucene and Aspect Oriented Programming in action

[/quote]

 

aspects can simplify implementing the observer pattern. Is the SpaceWars example still included in AspectJ?

Basically you set a pointcut to intercept your set methods (or even assignment to the field - then you don't even need to code get/set methods) and it inserts your notification code.

Comment viewing options

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