Lab M01P01
With usage of the AOP (Aspect Oriented Programming), you can implement the cross-cutting concerns (security, transactions, auditing, performance monitoring, ...) in your application. It enables to modularize these concerns and apply (weave) them into application logic of your application. Many common concerns are already built in the Spring framework so always check the Spring capabilities before you start with your own implementation. This Lab demonstrates the basic usage of Spring AOP.
-
You need to add performance monitoring and tracing logs to your Repository classes. This is example of typical cross-cutting functionality, which can be implemented using Aspects and later on applied to your code as required.
-
First add Performance Monitoring Aspect. Have a look into the ite.librarymaster.aspect package. There is the RepositoryPerformanceMonitor class skeleton created for you. This class implements Performance monitoring aspect, so we need to tell Spring about it.
Hint:
Annotate the RepositoryPerformanceMonitor class with the @Aspect annotation. You need to use also @Component annotation as aspect must be defined as Spring bean.
-
Aspect should execute some functionality, which is called advice. There is incomplete monitor(..){...} method implementation already in the RepositoryPerformanceMonitor, where you need to implement required cross-cutting concern (performance monitoring). The Around Advice is what you need in order to start monitoring before target execution and stop monitoring after target method execution.
Add following implementation into monitor(..) method.
Monitor monitor = monitorFactory.start(name); try { return repositoryMethod.proceed(); } finally { monitor.stop(); logger.info(monitor.toString()); }Notice the monitor(..) method argument. There is reference to actual Join Point, which exposes the actual invocation context. So we can call target method which was selected by Pointcut: repositoryMethod.proceed().
-
You should also mark the monitor() method in the RepositoryPerformanceMonitor Aspect as Advice. Use @Around annotation with defined Pointcut, which should select all methods on each Repository bean.
@Around("execution(public * ite.librarymaster.*.*Repository+.*(..))") -
Finally, run the JpaBookRepositoryTest JUnit test and see logged messages on console.
-
If more, then one Aspect is applied on target, you cen define Aspect execution precedence by the Spring @Order(Ordered.HIGHEST_PRECEDENCE + 10) annotation.
@Aspect @Order(Ordered.HIGHEST_PRECEDENCE + 10) public class RepositoryPerformanceMonitor {...} -
How can you selectively disable CCC/Advices? Try to use Spring profiles.