Performance monitoring (#974)
* spring performance monitoring interceptors * added person service
This commit is contained in:
parent
adccc13c32
commit
60da0a9542
|
@ -1,6 +1,8 @@
|
||||||
package com.baeldung.performancemonitor;
|
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.aspectj.lang.annotation.Pointcut;
|
||||||
import org.springframework.aop.Advisor;
|
import org.springframework.aop.Advisor;
|
||||||
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
|
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
|
||||||
|
@ -14,10 +16,10 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||||
@EnableAspectJAutoProxy
|
@EnableAspectJAutoProxy
|
||||||
public class AopConfiguration {
|
public class AopConfiguration {
|
||||||
|
|
||||||
@Pointcut("execution(public int com.baeldung.performancemonitor.Person.getAge())")
|
@Pointcut("execution(public String com.baeldung.performancemonitor.PersonService.getFullName(..))")
|
||||||
public void monitor() { }
|
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() { }
|
public void myMonitor() { }
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -34,7 +36,12 @@ public class AopConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Person person(){
|
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
|
@Bean
|
||||||
|
|
|
@ -4,10 +4,7 @@ import java.util.Date;
|
||||||
|
|
||||||
import org.aopalliance.intercept.MethodInvocation;
|
import org.aopalliance.intercept.MethodInvocation;
|
||||||
import org.apache.commons.logging.Log;
|
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.aop.interceptor.AbstractMonitoringInterceptor;
|
||||||
import org.springframework.util.StopWatch;
|
|
||||||
|
|
||||||
public class MyPerformanceMonitorInterceptor extends AbstractMonitoringInterceptor {
|
public class MyPerformanceMonitorInterceptor extends AbstractMonitoringInterceptor {
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,9 @@ public class PerfomanceApp {
|
||||||
|
|
||||||
ApplicationContext context = new AnnotationConfigApplicationContext(AopConfiguration.class);
|
ApplicationContext context = new AnnotationConfigApplicationContext(AopConfiguration.class);
|
||||||
Person person = (Person) context.getBean("person");
|
Person person = (Person) context.getBean("person");
|
||||||
|
PersonService personService = (PersonService) context.getBean("personService");
|
||||||
|
|
||||||
person.setAge(20);
|
System.out.println("Name is:"+personService.getFullName(person));
|
||||||
System.out.println("Age is:"+person.getAge());
|
System.out.println("Age is:"+personService.getAge(person));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,42 @@
|
||||||
package com.baeldung.performancemonitor;
|
package com.baeldung.performancemonitor;
|
||||||
|
|
||||||
public class Person {
|
import java.time.LocalDate;
|
||||||
private int age;
|
|
||||||
|
|
||||||
public int getAge() {
|
public class Person {
|
||||||
return age;
|
private String lastName;
|
||||||
|
private String firstName;
|
||||||
|
private LocalDate dateOfBirth;
|
||||||
|
|
||||||
|
public Person() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAge(int age) {
|
public Person(String firstName, String lastName, LocalDate dateOfBirth) {
|
||||||
this.age = age;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue