Aspect-oriented programming AOP is programming paradigm with which you get flexibility of adding additional code (such as logging, security etc), without modifying your existing. This increase the modularity and the injectable code can be reused in many other places. AOP concept has few important terms.

In this blog we are going to discuss on both approached Spring AOP and ApspectJ. AspectJ has set the ground for AOP programing while Spring AOP has extended the concept and made it simpler for there use. With Spring you can either go with Spring AOP or AspectJ both. In recent release Spring AOP has adapted the AspectJ annotation with that you will not have difficulty in switching from one to another. Here are some key differences between Spring AOP and Aspect J.

Here is an example.

@Aspect
public class CustomLogging {

   @PointCut("execution(* com.xyz.*.*(..))")
   private void allMethodsInPackage(){}

   @Before("allMethodsInPackage()")
   public void beforeAdvice(JoinPoint joinPoint){
     System.out.println("Method called is : " + joinPoint.getSignature().getName());
      System.out.println("Going to setup student profile.");
   }  

    @AfterReturning(
    pointcut = "execution(* com.xyz.bo.EmployeeBO.*(..))",
    returning= "result")
    public void afterReturningAdvice(JoinPoint joinPoint, Object result){
      System.out.println("Method called is : " + joinPoint.getSignature().getName());
      System.out.println("Employee id : " + ((Employee)result.getEmployeeId());
    }

    @AfterThrowing(
    pointcut = "execution(* com.xyz.bo.EmployeeBO.*(..))",
    throwing= "error")
    public void afterThrowingAdvice(JoinPoint joinPoint, Throwable error){
      System.out.println("Method called is : " + joinPoint.getSignature().getName());
      System.out.println("Erros is : " + error);
    }

    ...
    @Around // It will be before and after of joinpoints.
    @After // It will be after of joinpoints. 

}

Enabling AOP

Enabling Spring AOP.

It usage the proxy “org.springframework.aop.framework.ProxyFactoryBean” to proxy our objects and helps to inject the custom methods.

public class ProxyFactoryBean{  
  private Object target;  
  private List interceptorNames;  
...
}  

public class BeforeAdvisor implements MethodBeforeAdvice{  
    @Override  
    public void before(Method method, Object[] args, Object target)throws Throwable {  
        System.out.println("method info:"+method.getName()+" "+method.getModifiers());  
        System.out.println("target object class name: "+target.getClass().getName());  
    }  
    ...
}  
<bean id="someClass" class="com.xyz.SomeClass"></bean>  
<bean id="beforAdvice" class="com.xyz.BeforeAdvisor"></bean>  
  
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  <property name="target" ref="someClass"></property>  
  <property name="interceptorNames">  
    <list>  
      <value>beforAdvice</value>  
    </list>  
  </property>  
</bean>

Enabling Spring with AspectJ

pom.xml will have following dependencies.

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.6.11</version>
</dependency>

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.6.11</version>
</dependency>

<!-- Or just add following-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Enable AspectJ in XML by adding in configuration XML and @EnableAspectJAutoProxy to Enable AspectJ using Java in any configuration class.