Best practices of AOP proxies

We learned about AOP proxies and how AOP proxies work. We learned about different types of proxies in Spring AOP. The following are best practices to be followed while implementing AOP proxies in Spring:

  • Unless we need to perform a manipulative action at runtime or we want to gain fine-grained control of our proxies, use the declarative method of proxy configuration over the programmatic method.
  • Spring recommends JDK dynamic proxy over the CGLIB proxy wherever possible. If we are building our application from scratch and there is no requirement to create proxies of third-party APIs, implement the abstraction layer to loosely-couple the implementation using the interface and let Spring use the JDK dynamic proxy mechanism to create proxies.
  • In case of CGLIB proxies, make sure the methods are not final, as final methods cannot be overridden and hence they cannot be advised.
  • According to Spring, it is not possible to have aspects themselves be the target of advice from other aspects. There are workarounds for this situation; move the aspect method to a  new Spring bean annotated with @Component@Autowire this new Spring bean to aspect, and just call the advised method. MethodProfilingAspect is aspect defining a pointcut on all join points under com.packt.springhighperformance.ch3.bankingapp:
@Aspect
public class MethodProfilingAspect {

@Around("execution(*
com.packt.springhighperformance.ch3.bankingapp.*.*(..))")
public Object log(ProceedingJoinPoint joinPoint){
System.out.println("Before
Around"+joinPoint.getTarget().getClass().getName());
Object retVal = null;
try {
retVal = joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("After
Around"+joinPoint.getTarget().getClass().getName());
return retVal;
}
  • The following ValidatingAspect is aspect defined under the com.packt.springhighperformance.ch3.bankapp package, however a call to the validate method would not be advised by MethodProfilingAspect:
@Aspect
public class ValidatingAspect {

@Autowired
private ValidateService validateService;

@Before("execution(*
com.packt.springhighperformance.ch3.bankingapp.TransferService.tran
sfe r(..))")
public void validate(JoinPoint jp){
validateService.validateAccountNumber();
}
}
  • The following is the solution by creating a separate class with the @Component annotation and implementing the validate method. This class will be a Spring-managed bean and it will be advised:
@Component
public class ValidateDefault{

@Autowired
private ValidateService validateService;
public void validate(JoinPoint jp){
validateService.validateAccountNumber();
}
}
  • The following code of ValidatingAspect injects the ValidateDefault Spring bean and calls the validate method:
@Aspect
public class ValidatingAspect {

@Autowired
private ValidateDefault validateDefault;

@Before("execution(* com.packt.springhighperformance.ch3.bankingapp.TransferService.transfer(..))")
public void validate(JoinPoint jp){
 validateDefault.validate(jp);
}
}

Never implement fine-grained requirements through AOP or use AspectJ for such requirements. Do not use @Configurable on Spring-managed bean classes, otherwise it would do double initialization, once through the container and once through the aspect.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset