Toster.java
package com.toster.dao;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Toster {
}
TosterAspect.java
package com.toster.dao;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Aspect
public class TosterAspect {
private static final Logger LOG = LoggerFactory.getLogger(TosterAspect.class);
@Around("execution(* *(..)) && @annotation(tr)")
public Object request(final ProceedingJoinPoint pjp, final Toster tr) throws Throwable {
try {
LOG.info("Invoke {}", pjp.toShortString());
final Object proceed = pjp.proceed();
LOG.info("Return {}", proceed);
return proceed;
} catch (Exception e) {
LOG.error("Error", e);
throw e; //TODO
}
}
}
AspectConfiguration.java
package com.toster.dao;
import org.aspectj.lang.Aspects;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
public class AspectConfiguration {
@Bean
public TosterAspect tosterAspect() {
return Aspects.aspectOf(TosterAspect.class);
}
}