diff --git a/aspectj/pom.xml b/aspectj/pom.xml index 2fca4031fb..6e7ef1b961 100644 --- a/aspectj/pom.xml +++ b/aspectj/pom.xml @@ -44,7 +44,37 @@ junit ${junit.version} - + + + org.springframework + spring-context + 4.3.4.RELEASE + + + org.springframework + spring-beans + 4.3.4.RELEASE + + + org.springframework + spring-core + 4.3.4.RELEASE + + + cglib + cglib + 3.2.4 + + + org.springframework + spring-aop + 4.3.4.RELEASE + + + log4j + log4j + 1.2.17 + diff --git a/aspectj/src/main/java/com/baeldung/performancemonitor/AopConfiguration.java b/aspectj/src/main/java/com/baeldung/performancemonitor/AopConfiguration.java new file mode 100644 index 0000000000..3ebe9a7fd8 --- /dev/null +++ b/aspectj/src/main/java/com/baeldung/performancemonitor/AopConfiguration.java @@ -0,0 +1,52 @@ +package com.baeldung.performancemonitor; + +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.aop.Advisor; +import org.springframework.aop.aspectj.AspectJExpressionPointcut; +import org.springframework.aop.interceptor.PerformanceMonitorInterceptor; +import org.springframework.aop.support.DefaultPointcutAdvisor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; + +@Configuration +@EnableAspectJAutoProxy +public class AopConfiguration { + + @Pointcut("execution(public int com.baeldung.performancemonitor.Person.getAge())") + public void monitor() { } + + @Pointcut("execution(public void com.baeldung.performancemonitor.Person.setAge(int))") + public void myMonitor() { } + + @Bean + public PerformanceMonitorInterceptor performanceMonitorInterceptor() { + return new PerformanceMonitorInterceptor(true); + } + + @Bean + public Advisor performanceMonitorAdvisor() { + AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); + pointcut.setExpression("com.baeldung.performancemonitor.AopConfiguration.monitor()"); + return new DefaultPointcutAdvisor(pointcut, performanceMonitorInterceptor()); + } + + @Bean + public Person person(){ + return new Person(); + } + + @Bean + public MyPerformanceMonitorInterceptor myPerformanceMonitorInterceptor() { + return new MyPerformanceMonitorInterceptor(true); + } + + @Bean + public Advisor myPerformanceMonitorAdvisor() { + AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); + pointcut.setExpression("com.baeldung.performancemonitor.AopConfiguration.myMonitor()"); + return new DefaultPointcutAdvisor(pointcut, myPerformanceMonitorInterceptor()); + } + +} diff --git a/aspectj/src/main/java/com/baeldung/performancemonitor/MyPerformanceMonitorInterceptor.java b/aspectj/src/main/java/com/baeldung/performancemonitor/MyPerformanceMonitorInterceptor.java new file mode 100644 index 0000000000..c036359851 --- /dev/null +++ b/aspectj/src/main/java/com/baeldung/performancemonitor/MyPerformanceMonitorInterceptor.java @@ -0,0 +1,42 @@ +package com.baeldung.performancemonitor; + +import java.util.Date; + +import org.aopalliance.intercept.MethodInvocation; +import org.apache.commons.logging.Log; +import org.apache.log4j.Logger; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.aop.interceptor.AbstractMonitoringInterceptor; +import org.springframework.util.StopWatch; + +public class MyPerformanceMonitorInterceptor extends AbstractMonitoringInterceptor { + + public MyPerformanceMonitorInterceptor() { + } + + public MyPerformanceMonitorInterceptor(boolean useDynamicLogger) { + setUseDynamicLogger(useDynamicLogger); + } + + @Override + protected Object invokeUnderTrace(MethodInvocation invocation, Log log) throws Throwable { + + String name = createInvocationTraceName(invocation); + long start = System.currentTimeMillis(); + log.info("Method "+name+" execution started at:"+new Date()); + try { + return invocation.proceed(); + } + finally { + long end = System.currentTimeMillis(); + long time = end - start; + log.info("Method "+name+" execution lasted:"+time+" ms"); + log.info("Method "+name+" execution ended at:"+new Date()); + + if (time > 10){ + log.warn("Method execution longer than 10 ms!"); + } + + } + } +} diff --git a/aspectj/src/main/java/com/baeldung/performancemonitor/PerfomanceApp.java b/aspectj/src/main/java/com/baeldung/performancemonitor/PerfomanceApp.java new file mode 100644 index 0000000000..62b8287649 --- /dev/null +++ b/aspectj/src/main/java/com/baeldung/performancemonitor/PerfomanceApp.java @@ -0,0 +1,16 @@ +package com.baeldung.performancemonitor; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class PerfomanceApp { + public static void main(String[] args) { + + ApplicationContext context = new AnnotationConfigApplicationContext(AopConfiguration.class); + Person person = (Person) context.getBean("person"); + + person.setAge(20); + System.out.println("Age is:"+person.getAge()); + + } +} diff --git a/aspectj/src/main/java/com/baeldung/performancemonitor/Person.java b/aspectj/src/main/java/com/baeldung/performancemonitor/Person.java new file mode 100644 index 0000000000..02c779e6fa --- /dev/null +++ b/aspectj/src/main/java/com/baeldung/performancemonitor/Person.java @@ -0,0 +1,13 @@ +package com.baeldung.performancemonitor; + +public class Person { + private int age; + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/aspectj/src/main/resources/log4j.properties b/aspectj/src/main/resources/log4j.properties new file mode 100644 index 0000000000..9e2afcd5b0 --- /dev/null +++ b/aspectj/src/main/resources/log4j.properties @@ -0,0 +1,10 @@ +log4j.rootLogger=TRACE, stdout + +# Redirect log messages to console +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n + +log4j.logger.org.springframework.aop.interceptor.PerformanceMonitorInterceptor=TRACE, stdout +log4j.logger.com.baeldung.performancemonitor.MyPerformanceMonitorInterceptor=INFO, stdout \ No newline at end of file