Spring AOP AspectJ Logging Example


 

Spring AOP AspectJ Logging Example

In this tutorial, you will learn how to use AspectJ in spring for making log in spring application.

In this tutorial, you will learn how to use AspectJ in spring for making log in spring application.

Spring AOP AspectJ Logging Example

@AspectJ is a style of declaring a aspects as a regular java classes. To implement AspectJ in your application you need to configure config.xml file in spring. For example:-
<aop:aspect id="aopAspect" ref="aspectLogging">
                <aop:before pointcut-ref="pointcutLog" method="logEntry" />
</aop:aspect>

SimpleBean.java

package roseindia.net.bean;

public class SimpleBean {
	private String name;
	private int age;

	public SimpleBean() {
	}

	public SimpleBean(final String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String setNameAndAge() {
		return this.name + " (" + this.age + ")";
	}
}

AspectLogger.java

package roseindia.net.logger;

import org.aspectj.lang.JoinPoint;

public class AspectLogger {
	public void logEntry(JoinPoint joinPoint) {
		log("Currently in " + joinPoint.getSignature().getName() + "()");
	}

	public void logExit(JoinPoint joinPoint) {
		log("Currently in " + joinPoint.getSignature().getName() + "()");
	}

	public void logAroundAdvice(JoinPoint joinPoint) {
		log("Currently in " + joinPoint.getSignature().getName() + "()");
	}

	public void logExitAfterReturn(JoinPoint joinPoint) {
		log("Currently in " + joinPoint.getSignature().getName() + "()");
	}

	public void logAfterThrowsAdvice(JoinPoint joinPoint) {
		log("Currently in " + joinPoint.getSignature().getName() + "()");
	}

	public static void log(String LogMessage) {
		System.err.println(LogMessage);
	}
}

ContentClass.java

package roseindia.net.logger;

import roseindia.net.bean.SimpleBean;

public class ContentClass {
	public static void loggerDetail(SimpleBean simpleBean) {
		System.out.println("Student Logger Detail");
		System.out
				.println("**************************************************************");
		System.out.println("Name: " + simpleBean.getName());
		System.out.println("Age: " + simpleBean.getAge());
		System.out.println("Name (Age): " + simpleBean.setNameAndAge());
		System.out
				.println("**************************************************************");
	}
}

MainClaz.java

package roseindia.net.main;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import roseindia.net.bean.SimpleBean;
import roseindia.net.logger.ContentClass;

public class MainClaz {
	public static void main(String[] args) {
		ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"config.xml");
		SimpleBean simpleBean = (SimpleBean) applicationContext
				.getBean("simpleBean");
		ContentClass contentClass = new ContentClass();
		contentClass.loggerDetail(simpleBean);
		applicationContext.close();
	}
}

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

  <!-- Bean Mapping -->
  <bean id="simpleBean" class="roseindia.net.bean.SimpleBean">
    <constructor-arg value="Vinay" />
    <constructor-arg value="24" />
  </bean>

  <!-- Aspect Mapping -->
  <bean id="aspectLogging" class="roseindia.net.logger.AspectLogger" />

  <aop:config>

    <!-- Pointct Mapping -->
    <aop:pointcut id="pointcutLog" expression="within(roseindia.net.bean.SimpleBean)" />

    <!-- Advice Mapping -->
    <aop:aspect id="aopAspect" ref="aspectLogging">
      <aop:before pointcut-ref="pointcutLog" method="logEntry" />
      <aop:after pointcut-ref="pointcutLog" method="logExit" />
      <aop:after-returning pointcut-ref="pointcutLog"
        method="logExitAfterReturn" />
<!--      <aop:around pointcut-ref="pointcutLog" method="logAroundAdvice"/>-->
      <aop:after-throwing pointcut-ref="pointcutLog" method="logAfterThrowsAdvice"/>
    </aop:aspect>

  </aop:config>

</beans>
When you run this application it will display message as shown below:
Student Logger Detail
*************************************************************
Currently in getName()
Currently in getName()
Currently in getName()
Name: Vinay
Currently in getAge()
Currently in getAge()
Currently in getAge()
Currently in setNameAndAge()
Currently in setNameAndAge()
Currently in setNameAndAge()
Age: 24
Name (Age): Vinay (24)
**************************************************************

Download this example code

Ads