Spring implements AOP using AspectJ annotations and XML configuration

  • 2020-05-10 18:17:45
  • OfStack

This article demonstrates how AOP is implemented in Spring using AspectJ annotations and XML configuration

Below is the Java Project implementation of AOP using the AspectJ annotation
The first is the applicationContext.xml file located under classpath


<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
   
  <!--  To enable the AspectJ right Annotation The support of  -->     
  <aop:aspectj-autoproxy/> 
       
  <bean id="userManager" class="com.jadyer.annotation.UserManagerImpl"/> 
   
  <bean id="securityHandler" class="com.jadyer.annotation.SecurityHandler"/> 
</beans> 

Then there are the service layer interfaces and the implementation classes


package com.jadyer.annotation; 
public interface UserManager { 
  public void addUser(String username, String password); 
  public void delUser(int userId); 
  public String findUserById(int userId); 
  public void modifyUser(int userId, String username, String password); 
} 
 
/** 
 *  The above UserManager Is the interface of the service layer  
 *  The following UserManagerImpl Is the implementation class of the service layer interface  
 */ 
 
package com.jadyer.annotation; 
 
public class UserManagerImpl implements UserManager { 
  public void addUser(String username, String password) { 
    System.out.println("------UserManagerImpl.addUser() is invoked------"); 
  } 
 
  public void delUser(int userId) { 
    System.out.println("------UserManagerImpl.delUser() is invoked------"); 
  } 
 
  public String findUserById(int userId) { 
    System.out.println("------UserManagerImpl.findUserById() is invoked------"); 
    return " Tough life "; 
  } 
 
  public void modifyUser(int userId, String username, String password) { 
    System.out.println("------UserManagerImpl.modifyUser() is invoked------"); 
  } 
} 

Next is the entry class annotated with the AspectJ annotation


package com.jadyer.annotation; 
 
import org.aspectj.lang.annotation.After; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Pointcut; 
 
@Aspect 
public class SecurityHandler { 
  /** 
   *  define Pointcut 
   * @see Pointcut For the name of the addAddMethod() , this method has no return values or arguments  
   * @see  The method is 1 Is not called  
   */ 
  @Pointcut("execution(* add*(..))") // Match all with add Opening method  
  private void addAddMethod(){}; 
   
  /** 
   *  define Advice 
   * @see  Represents our Advice To what Pointcut Subscribe to the Joinpoint on  
   */ 
  //@Before("addAddMethod()") 
  @After("addAddMethod()") 
  private void checkSecurity() { 
    System.out.println("------ 【 checkSecurity is invoked 】 ------"); 
  }     
} 

Finally, the client test class


package com.jadyer.annotation; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
/** 
 * Spring right AOP Support: adoption Annotation way  
 * @see ------------------------------------------------------------------------------------- 
 * @see Spring To provide the AOP It's still very powerful, configurable, and its default implementation USES JDK A dynamic proxy  
 * @see  use Spring the AOP You don't need to inherit anything related, and you don't need to implement the interface  
 * @see  But on one condition: because it is JDK Dynamic proxy, so this class must be implemented if you want to generate a proxy 1 Just an interface  
 * @see  If this class doesn't have implements Use the interface Spring The default AOP When you implement, then you get an error  
 * @see  Usually the classes that need to generate proxies are service-layer classes, so they are usually drawn 1 The interface comes out. That is, develop the habit of programming to interfaces  
 * @see ------------------------------------------------------------------------------------- 
 * @see  using Annotation Way to complete AOP The basic steps for the example are as follows  
 * @see 1 , Spring2.0 Dependent package configuration. new Annotation support  
 * @see   * SPRING_HOME//dist//spring.jar 
 * @see   * SPRING_HOME//lib//log4j//log4j-1.2.14.jar 
 * @see   * SPRING_HOME//lib//jakarta-commons//commons-logging.jar 
 * @see   * SPRING_HOME//lib//aspectj//*.jar 
 * @see 2 , modularize and set up the crosscutting concerns SecurityHandler.java 
 * @see 3 , specified by annotation SecurityHandler for Aspect 
 * @see 4 , using annotation definition Advice and Pointcut 
 * @see 5 , enabling AspectJ right Annotation Support, and will target classes and Aspect Class configuration to IoC In the container  
 * @see 6 Develop the client  
 * @see ------------------------------------------------------------------------------------- 
 */ 
public class Client { 
  public static void main(String[] args) { 
    ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    UserManager userManager = (UserManager)factory.getBean("userManager"); 
    userManager.addUser(" Kylin zhang ", "02200059"); 
  } 
} 

Below is the Java Project implementation of AOP using the XML configuration file
The first is the applicationContext-cglib.xml file located in the src root directory


<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
       
  <!--  Forced to use CGLIB The agent  --> 
  <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> --> 
   
  <bean id="userManager" class="com.jadyer.cglib.UserManagerImpl"/> 
   
  <bean id="securityHandler" class="com.jadyer.cglib.SecurityHandler"/> 
   
  <aop:config> 
    <aop:aspect id="securityAspect" ref="securityHandler">  
      <aop:pointcut id="addAddMethod" expression="execution(* add*(..))"/> 
      <aop:before method="checkSecurity" pointcut-ref="addAddMethod"/> 
    </aop:aspect> 
  </aop:config> 
</beans> 
 
<!--  
 matching add All the way to the beginning  
execution(* add*(..)) 
 
 matching com.jadyer.servcices.impl All methods of all classes under the package  
execution(* com.jadyer.servcices.impl.*.*(..)) 
 
 matching com.jadyer.servcices.impl Under the bag add or del All the way to the beginning  
execution(* com.jadyer.servcices.impl.*.add*(..)) || execution(* com.jadyer.servcices.impl.*.del*(..)) 
 --> 

Then there are the service layer interfaces and the implementation classes


package com.jadyer.cglib; 
public interface UserManager { 
  public void addUser(String username, String password); 
  public void delUser(int userId); 
  public String findUserById(int userId); 
  public void modifyUser(int userId, String username, String password); 
} 
 
/** 
 *  The above UserManager It's a service layer interface  
 *  The following UserManagerImpl Is the implementation class of the service layer interface  
 */ 
 
package com.jadyer.cglib; 
 
public class UserManagerImpl {  
//implements UserManager { 
  public void addUser(String username, String password) { 
    System.out.println("------UserManagerImpl.addUser() is invoked------"); 
  } 
 
  public void delUser(int userId) { 
    System.out.println("------UserManagerImpl.delUser() is invoked------"); 
  } 
 
  public String findUserById(int userId) { 
    System.out.println("------UserManagerImpl.findUserById() is invoked------"); 
    return " zhang 3"; 
  } 
 
  public void modifyUser(int userId, String username, String password) { 
    System.out.println("------UserManagerImpl.modifyUser() is invoked------"); 
  } 
} 

This is followed by the entry class specified in applicationContext-cglib.xml


package com.jadyer.cglib; 
 
import org.aspectj.lang.JoinPoint; 
 
/** 
 *  The client invocation information is passed to this Advice In the  
 * @see  Can be found in Advice add 1 a JoinPoint Parameter, get the method name and parameter value of the client call  
 * @see  Pure use in the future AOP It's rare to write something like this, but we mostly use it Spring Provided transactions  
 * @see  You know that. Here is the sample code  
 */ 
public class SecurityHandler { 
  private void checkSecurity(JoinPoint joinPoint) { 
    for (int i=0; i<joinPoint.getArgs().length; i++) { 
      System.out.println(joinPoint.getArgs()[i]); // Gets the parameter value of the method invoked by the client  
    } 
    System.out.println(joinPoint.getSignature().getName()); // Gets the name of the method invoked by the client  
    System.out.println("------ 【 checkSecurity is invoked 】 ------"); 
  } 
} 

Finally, the client test class


package com.jadyer.cglib; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
/** 
 * @see -------------------------------------------------------------------------------------------------- 
 * @see JDK Dynamic proxy and CGLIB Agency difference  
 * @see 1..JDK A dynamic proxy ACTS as a proxy for a class that implements the interface  
 * @see 2..CGLIB A proxy can proxy a class, primarily for a specified class 1 A subclass. Because it is inherited, it is best not to use the target class final The statement  
 * @see -------------------------------------------------------------------------------------------------- 
 * @see  The choice of agency mode  
 * @see 1.. If the target object implements the interface, it does so by default JDK Dynamic proxy implementation AOP Can be used compulsively CGLIB Generate proxy implementation AOP 
 * @see 2.. If the target object does not implement the interface, it must be introduced CGLIB , at this moment Spring Will be in JDK Dynamic proxy and CGLIB Automatic switching between agents  
 * @see 3.. Comparison encourages business objects to be programmed against interfaces, so use is encouraged JDK Dynamic proxy. Because the target we represent, 1 These are usually business objects  
 * @see -------------------------------------------------------------------------------------------------- 
 * @see  use CGLIG Steps for the agent  
 * @see 1.. new CGLIB Library: SPRING_HOME//lib//cglib//*.jar 
 * @see 2.. New configuration label for mandatory use CGLIB The agent <aop:aspectj-autoproxy proxy-target-class="true"/> 
 * @see -------------------------------------------------------------------------------------------------- 
 */ 
public class Client { 
  public static void main(String[] args) { 
    ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext-cglib.xml"); 
     
    // when UserManagerImpl To achieve the UserManager In the case of the interface, at this point Spring Will be used automatically JDK A dynamic proxy  
    // If the project has been introduced cglib Library and enforce it in the configuration file CGLIB Agent, at this point Spring Will use CGLIB The agent  
    //UserManager userManager = (UserManager)factory.getBean("userManager"); 
     
    // As a result of UserManagerImpl It didn't happen UserManager Interface, so the receive type can no longer be used UserManager interface  
    // And it has been introduced in the project cglib Library, although it is not mandatory in the configuration file CGLIB The agent, but Spring Will be used automatically CGLIB The agent     
    UserManagerImpl userManager = (UserManagerImpl)factory.getBean("userManager"); 
     
    userManager.addUser(" wu 3 province ", "02200059"); 
  } 
} 

Related articles: