Auto tracing of all the classes in package using Aspects

package com.stb.async; import org.aspectj.lang.annotation.Pointcut; import org.springframework.aop.Advisor; import org.springframework.aop.aspectj.AspectJExpressionPointcut; import org.springframework.aop.interceptor.CustomizableTraceInterceptor; 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; /** * Additional Configuration for {@link CustomizableTraceInterceptor} to be used for custom logging * <br/> * The Interceptor will be applied to public methods declared in {@link org.springframework.data.repository.Repository} * */ @Configuration @EnableAspectJAutoProxy public class LoggingConfiguration { private static final String POINTCUT_EXECUTION_REPOSITORY = "execution(public * (com.stb..*).*(..))"; private static final String EXCEPTION_MESSAGE = "Exception thrown: " + CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION; private static final String EXIT_METHOD_MESSAGE = "Exiting method: " + CustomizableTraceInterceptor.PLACEHOLDER_METHOD_NAME + " having return value " + CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE + ", execution time: " + CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME + " ms"; private static final String ENTER_METHOD_MESSAGE = "Entering method: " + CustomizableTraceInterceptor.PLACEHOLDER_METHOD_NAME + "(" + CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENTS + ")"; @Bean public CustomizableTraceInterceptor interceptor() { CustomizableTraceInterceptor interceptor = new CustomizableTraceInterceptor(); interceptor.setEnterMessage(ENTER_METHOD_MESSAGE); interceptor.setExceptionMessage(EXCEPTION_MESSAGE); interceptor.setExitMessage(EXIT_METHOD_MESSAGE); return interceptor; } @Pointcut("execution(public * (@org.springframework.stereotype.Service com.stb..*).*(..))") public void serviceAnnotation() { } @Pointcut("execution(public * (@org.springframework.stereotype.Repository com.stb..*).*(..))") public void repositoryAnnotation() {} @Pointcut("execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))") public void jpaRepository() {} @Pointcut("serviceAnnotation() || repositoryAnnotation() || jpaRepository()") public void performanceMonitor() {} @Bean public PerformanceMonitorInterceptor performanceMonitorInterceptor() { return new PerformanceMonitorInterceptor(true); } /* @Bean public Advisor performanceMonitorAdvisor() { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression("com.platforms.integration.api.logging.customlogging.LoggingConfiguration.performanceMonitor()"); return new DefaultPointcutAdvisor(pointcut, performanceMonitorInterceptor()); }*/ @Bean public Advisor traceAdvisor() { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(POINTCUT_EXECUTION_REPOSITORY); //pointcut.setExpression("execution(public * org.springframework.data.repository.Repository+POINTCUT_EXECUTION_REPOSITORY+.*(..))"); System.out.println("Inside trace advisor"); return new DefaultPointcutAdvisor(pointcut, interceptor()); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.