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.@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"?>
|
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) ************************************************************** |