Examples of implementation methods for defining pointcuts in spring AOP

Time:2020-9-16

This paper describes the implementation method of defining pointcuts in spring AOP. For your reference, the details are as follows:

1. Configuration

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
   Automatically search component class
   <context:component-scan
      base-package="org.crazyit.app.service
      ,org.crazyit.app.aspect">
      <context:include-filter type="annotation"
        expression="org.aspectj.lang.annotation.Aspect" />
   </context:component-scan>
   <! -- start @ AspectJ support -- >
   <aop:aspectj-autoproxy />
</beans>

Dihedral class

SystemArchitecture


package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
@Aspect
public class SystemArchitecture
{
   @Pointcut("execution(* org.crazyit.app.service.impl.*.*(..))")
   public void myPointcut(){}
}

LogAspect

package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
@Aspect
public class LogAspect
{
  //Directly use the mypointcut() pointcut of systemarchitecture aspect class
  @AfterReturning(returning="rvt"
    , pointcut="SystemArchitecture.myPointcut()")
  //When RVT is declared, the specified type restricts the target method from returning a value of the specified type or not
  //Here, the type of RVT is declared as object, which means that there is no restriction on the return value of the target method
  public void log(Object rvt)
  {
    System.out.println ("get target method return value: + RVT)";
    System.out.println (simulation log function...);
  }
}

Three interfaces

Hello

package org.crazyit.app.service;
public interface Hello {
   //Define a simple method to simulate the business logic method in application
   void foo();
   //Define an adduser() method to simulate the method of adding users in the application
   int addUser(String name, String pass);
}

World

package org.crazyit.app.service;
public interface World {
   //Define a simple method to simulate the business logic method in application
   public void bar();
}

4. Implementation class

HelloImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("hello")
public class HelloImpl implements Hello
{
  //Define a simple method to simulate the business logic method in application
  public void foo()
  {
    System.out.println ("execute the foo() method of the Hello component");
  }
  //Define an adduser() method to simulate the method of adding users in the application
  public int addUser(String name , String pass)
  {
    System.out.println ("addUser adding user to execute Hello component: + name)";
    if(name.length() < 3 || name.length() > 10)
    {
      Throw new illegalargumentexception ("the length of the name parameter must be greater than 3 and less than 10! "";
    }
    return 20;
  }
}

WorldImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("world")
public class WorldImpl implements World {
  //Define a simple method to simulate the business logic method in application
  public void bar() {
    System.out.println ("execute the bar () method of the world component");
  }
}

Five types of testing

package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class BeanTest
{
  public static void main(String[] args)
  {
    //Create spring container
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    Hello hello = ctx.getBean("hello" , Hello.class);
    hello.foo();
    hello.addUser ("Monkey King", "7788");
    World world = ctx.getBean("world" , World.class);
    world.bar();
  }
}

6. Test results

Execute the foo () method of the Hello component
Get target method return value: null
Simulation logging function
Add user to add user: Monkey King
Get target method return value: 20
Simulation logging function
Execute the bar () method of the world component
Get target method return value: null
Simulation logging function

For more Java related content, interested readers can view the special topics of this website: introduction and advanced course of spring framework, course of Java data structure and algorithm, summary of Java operating DOM node skills, summary of java file and directory operation skills and summary of Java cache operation skills

I hope that this paper will be helpful to Java programming.