概念解释

1、AOP面向方面的编程。就是讲编程逻辑拆分成不同的方法。而这些方法就是横切关注点。注意,AOP编程的前提条件是OOP编程。
2、Aspect就是对横切关注点(method)的通用额外业务操作。如LoggingAspect.
3、Join point就是单个的方法,即是横切关注点。如:student.getAget();
4、Advice就是横切关注点的通知类型。如:before、after等。
5、Pointcut就是N个横切关注点。如:

6、Target object关注的对象。如:Student对象。

XML实例
一、被切面的目标对象

public class Student {
    private Integer age;
    private String name;

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        System.out.println("Age : " + age);
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        System.out.println("Name : " + name);
        return name;
    }

    public void printThrowException() {
        System.out.println("Exception raised");
        throw new IllegalArgumentException();
    }
}

二、切面module

public class LoggingAspect {

    public void beforeAdvice() {
        System.out.println("Going to setup student profile.");
    }

    public void afterAdvice() {
        System.out.println("Student profile has been setup.");
    }

    public void afterReturningAdvice(Object retVal) {
        System.out.println("Returning:" + retVal.toString());
    }

    public void AfterThrowingAdvice(IllegalArgumentException ex) {
        System.out.println("There has been an exception: " + ex.toString());
    }

    public void aroundAdvice() {
        System.out.println("Student profile around setup.");
    }
}

三、spring配置




      
    
        
        
    

    
        
            

            
            
            
            
            
        

    

四、测试

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
        Student student = context.getBean(Student.class);
        student.getName();
        student.getAge();
        student.printThrowException();
    }

五、依赖

    
      org.aspectj
      aspectjrt
      1.8.9
    

    
      org.aspectj
      aspectjweaver
      1.8.9
    

注解实例

一、被切面的目标对象

@Component
public class Student {
    private Integer age;
    private String name;

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        System.out.println("Age : " + age);
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        System.out.println("Name : " + name);
        return name;
    }

    public void printThrowException() {
        System.out.println("Exception raised");
        throw new IllegalArgumentException();
    }
}

二、切面module

@Aspect
@Component
public class AnnotatedLogging {

    @Pointcut("execution(* com.demo.enity.Student.getName(..))")
    private void getName() {
    }

    @Pointcut("execution(* com.demo.enity.Student.getAge(..))")
    private void getAge() {
    }

    @Pointcut("execution(* com.demo.enity.Student.*(..))")
    private void selectedAll() {
    }

    @Before("getName()")
    public void doBeforeTask() {
    }

    @After("getAge()")
    public void doAfterTask() {
    }

    @AfterReturning(pointcut = "getName()", returning = "retVal")
    public void doAfterReturnningTask(Object retVal) {
        System.out.println("AnnotatedLogging-->Returning:" + retVal.toString());
    }

    @AfterThrowing(pointcut = "selectedAll()", throwing = "ex")
    public void doAfterThrowingTask(Exception ex) {
        System.out.println("AnnotatedLogging-->There has been an exception: " + ex.toString());
    }

    @Around("getAge()")
     public void doAroundTask(ProceedingJoinPoint joinPoint) throws Throwable  {
            Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        joinPoint.proceed();
        System.out.println("AnnotatedLogging-->Student profile around setup.");
    }
}

三、spring配置




    

    

四、测试

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-annotation.xml");
        Student student = context.getBean(Student.class);
        student.getName();
        student.getAge();
        student.printThrowException();
    }

五、依赖

    
      org.aspectj
      aspectjrt
      1.8.9
    

    
      org.aspectj
      aspectjweaver
      1.8.9
    

参考:https://www.tutorialspoint.com/spring/aop_with_spring.htm