Skip to content

enginooby-practice/spring-aop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Practice Topics

Configuration

  1. Add dependencies:
    • Spring framework [URL]
    • AspectJ Weaver: supports AOP (avoid Beta version) [URL]
  2. Create Spring AOP Java configuaration class with @Configuration, @EnableAspectJAutoProxy, @ComponentScan [JavaConfig].
  3. Create bean class with @Component [Account].
  4. Create Main app class [LoggingApp]
    1. Read Spring config Java class to get the context
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
    
    1. Get bean class from Spring container
    BeanClass aBean = context.getBean("aBean", BeanClass.class);
    
    1. Call business methods of the bean
    2. Close the context: context.close();
  5. Create Aspect class with @Aspect and @Component (to group related Advices) [LoggingAspect]
    1. Setup logger [java.util.logging.Logger] or use @Logger from Lombok (to log Advices output in the same thread? as Spring)
    2. Setup Pointcut declarations with @Pointcut (to associate business methods of bean with Advices)
    3. Create Advices (to execute whatever code with associated business methods)

Concepts

  • @EnableAspectJAutoProxy
  • AnnotationConfigApplicationContext
  • Aspect with @Aspect
  • Advice [LoggingAspect]
    • @Before: run before the method
    • @AfterReturning: run after the method (success execution), use returning to get returning result
    • @AfterThrowing: run after the method (if exception thrown), use throwing and Throwable the capture the exception, can not handle exception
    • @After: run after the method (regardless of outcome)
    • @Around: run before and after the method, use ProceedingJoinPoint.proceed() to execute target method when desired, can handle/rethrow exception using try catch block
  • Pointcut expression: execution(return_type package.class.method(params))
    • Wildcards [LoggingAspect]
      • (): no args
      • (*): one arg, any types
      • (..): zero or more args, any types
      • (fully qualified classname)
    • Declare with @Pointcut [CommonAspect]
    • Combine pointcut expressions using &&, ||, ! [CommonAspect]
  • @Order: Decide the execution order of all Advices in a Aspect
    • Lower numbers have higher predence
    • Negative numbers are allowed
    • Does not have to be consecutive
  • JoinPoint: contains metadata of the business method [TestingAspect]
    • Get method signature by MethodSignature and JoinPoint.getSignature()
    • Get method parameters by Objec[] and JoinPoint.getArgs()

Notes - Tips

  • 📌 Quick copy fully qualified classname from that class
  • 📌 Group related advices into one Aspect to order with @Order (i.e., logging aspect, testing aspect)
  • 📌 Declare public pointcut expressions in a common Aspect to share with other Aspects [CommonAspect].
  • ℹ️ Order execution: @Before -> @Around (before) -> Business method -> @Around (after) -> @After -> @AfterThrowing

Releases

No releases published

Packages

No packages published

Languages