BAEL-3335 example of reading request multiple times. removed spring boot and using spring-webmvc

This commit is contained in:
sumit-bhawsar
2019-10-20 04:01:29 +01:00
parent db85c8f275
commit 2e97ec17f4
20450 changed files with 1641014 additions and 0 deletions
@@ -0,0 +1,54 @@
package com.baeldung.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;
@Component
@Aspect
public class LoggingAspect {
private static Logger logger = Logger.getLogger(LoggingAspect.class.getName());
private ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("[yyyy-mm-dd hh:mm:ss:SSS]");
}
};
@Pointcut("within(com.baeldung..*) && execution(* com.baeldung.dao.FooDao.*(..))")
public void repositoryMethods() {
}
@Pointcut("within(com.baeldung..*) && @annotation(com.baeldung.aop.annotations.Loggable)")
public void loggableMethods() {
}
@Pointcut("within(com.baeldung..*) && @args(com.baeldung.aop.annotations.Entity)")
public void methodsAcceptingEntities() {
}
@Before("repositoryMethods()")
public void logMethodCall(JoinPoint jp) {
String methodName = jp.getSignature().getName();
logger.info(sdf.get().format(new Date()) + methodName);
}
@Before("loggableMethods()")
public void logMethod(JoinPoint jp) {
String methodName = jp.getSignature().getName();
logger.info("Executing method: " + methodName);
}
@Before("methodsAcceptingEntities()")
public void logMethodAcceptionEntityAnnotatedBean(JoinPoint jp) {
logger.info("Accepting beans with @Entity annotation: " + jp.getArgs()[0]);
}
}
@@ -0,0 +1,32 @@
package com.baeldung.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
@Aspect
@Component
public class PerformanceAspect {
private static Logger logger = Logger.getLogger(PerformanceAspect.class.getName());
@Pointcut("within(com.baeldung..*) && execution(* com.baeldung.dao.FooDao.*(..))")
public void repositoryClassMethods() {
}
@Around("repositoryClassMethods()")
public Object measureMethodExecutionTime(ProceedingJoinPoint pjp) throws Throwable {
long start = System.nanoTime();
Object retval = pjp.proceed();
long end = System.nanoTime();
String methodName = pjp.getSignature().getName();
logger.info("Execution of " + methodName + " took " + TimeUnit.NANOSECONDS.toMillis(end - start) + " ms");
return retval;
}
}
@@ -0,0 +1,39 @@
package com.baeldung.aop;
import com.baeldung.events.FooCreationEvent;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class PublishingAspect {
private ApplicationEventPublisher eventPublisher;
@Autowired
public void setEventPublisher(ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}
@Pointcut("within(com.baeldung..*) && execution(* com.baeldung.dao.FooDao.*(..))")
public void repositoryMethods() {
}
@Pointcut("within(com.baeldung..*) && execution(* com.baeldung.dao.FooDao.create*(Long,..))")
public void firstLongParamMethods() {
}
@Pointcut("repositoryMethods() && firstLongParamMethods()")
public void entityCreationMethods() {
}
@AfterReturning(value = "entityCreationMethods()", returning = "entity")
public void logMethodCall(JoinPoint jp, Object entity) throws Throwable {
eventPublisher.publishEvent(new FooCreationEvent(entity));
}
}
@@ -0,0 +1,11 @@
package com.baeldung.aop.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Entity {
}
@@ -0,0 +1,11 @@
package com.baeldung.aop.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Loggable {
}