Performance monitoring (#974)

* spring performance monitoring interceptors

* added person service
This commit is contained in:
lor6 2017-01-08 20:21:31 +02:00 committed by KevinGilmore
parent adccc13c32
commit 60da0a9542
5 changed files with 66 additions and 16 deletions

View File

@ -1,6 +1,8 @@
package com.baeldung.performancemonitor;
import org.aspectj.lang.annotation.Aspect;
import java.time.LocalDate;
import java.time.Month;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
@ -14,10 +16,10 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy
public class AopConfiguration {
@Pointcut("execution(public int com.baeldung.performancemonitor.Person.getAge())")
@Pointcut("execution(public String com.baeldung.performancemonitor.PersonService.getFullName(..))")
public void monitor() { }
@Pointcut("execution(public void com.baeldung.performancemonitor.Person.setAge(int))")
@Pointcut("execution(public int com.baeldung.performancemonitor.PersonService.getAge(..))")
public void myMonitor() { }
@Bean
@ -34,7 +36,12 @@ public class AopConfiguration {
@Bean
public Person person(){
return new Person();
return new Person("John","Smith", LocalDate.of(1980, Month.JANUARY, 12));
}
@Bean
public PersonService personService(){
return new PersonService();
}
@Bean

View File

@ -4,10 +4,7 @@ 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 {

View File

@ -8,9 +8,9 @@ public class PerfomanceApp {
ApplicationContext context = new AnnotationConfigApplicationContext(AopConfiguration.class);
Person person = (Person) context.getBean("person");
PersonService personService = (PersonService) context.getBean("personService");
person.setAge(20);
System.out.println("Age is:"+person.getAge());
System.out.println("Name is:"+personService.getFullName(person));
System.out.println("Age is:"+personService.getAge(person));
}
}

View File

@ -1,13 +1,42 @@
package com.baeldung.performancemonitor;
import java.time.LocalDate;
public class Person {
private int age;
private String lastName;
private String firstName;
private LocalDate dateOfBirth;
public int getAge() {
return age;
public Person() {
}
public void setAge(int age) {
this.age = age;
public Person(String firstName, String lastName, LocalDate dateOfBirth) {
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.performancemonitor;
import java.time.LocalDate;
import java.time.Period;
public class PersonService {
public String getFullName(Person person){
return person.getLastName()+" "+person.getFirstName();
}
public int getAge(Person person){
Period p = Period.between(person.getDateOfBirth(), LocalDate.now());
return p.getYears();
}
}