DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Maximizing Efficiency With the Test Automation Pyramid: Leveraging API Tests for Optimal Results
  • The Missing Link in Cybersecurity: Culture
  • Microservices Governance and API Management
  • Secure Your Method Using AOP

Trending

  • Logging to Infinity and Beyond: How To Find the Hidden Value of Your Logs
  • How to Migrate Vector Data from PostgreSQL to MyScale
  • Creating a Custom Starter With Spring Boot 3
  • Memory Management in Java: An Introduction
  1. DZone
  2. Coding
  3. Languages
  4. Aspect Oriented Programming - A Different Aspect.

Aspect Oriented Programming - A Different Aspect.

Murat Yener user avatar by
Murat Yener
·
Jul. 24, 08 · News
Like (0)
Save
Tweet
Share
8.29K Views

Join the DZone community and get the full member experience.

Join For Free

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?
Aspect (computer programming) Aspect-oriented programming

Published at DZone with permission of Murat Yener. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Maximizing Efficiency With the Test Automation Pyramid: Leveraging API Tests for Optimal Results
  • The Missing Link in Cybersecurity: Culture
  • Microservices Governance and API Management
  • Secure Your Method Using AOP

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: