spring aop中aspect和advisor的区别

2020-12-27 13:27

阅读:905

标签:base   bsp   inter   asp   通知   proc   contex   stat   text   

在使用xml配置spring aop的时候一般有两种方式

 1    :基本上用于事务管理

 2    :日志 缓存 增强等功能

实现方式

   需要实现接口

    1 环绕切: 实现 MethodInterceptor 接口

    2 前切:实现 MethodBeforeAdvice 接口

    3 后切:实现 MethodAfterAdvice 接口

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class MyBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("执行前置通知");
    }
}
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MyAround implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("环绕-前置");
        Object proceed = methodInvocation.proceed();
        System.out.println("环绕-后置");
        return proceed;


    }
}
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class MyAfterAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行后置通知");
    }
}
public interface BaseServlet {
    void run();
}
public class MyServlet  implements  BaseServlet {

    @Override
    public void run() {
        System.out.println("MyServlet run");
    }
}
import com.my.servlet.BaseServlet;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopAdvisorApp {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("adviceApplicationContext.xml");
        BaseServlet myServlet = ac.getBean("myServlet", BaseServlet.class);
        myServlet.run();
    }
}
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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    bean id="myServlet" class="com.my.servlet.MyServlet"/>

    bean id="myBeforeAdvice" class="com.my.aop.advisor.MyBeforeAdvice"/>
    bean id="myAfterAdvice" class="com.my.aop.advisor.MyAfterAdvice"/>
    bean id="myAround" class="com.my.aop.advisor.MyAround"/>
    
    aop:config>
        aop:pointcut id="mypoint" expression="execution(* com.my.servlet.*.*(..))"/>
        aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="mypoint"/>
        aop:advisor advice-ref="myAfterAdvice" pointcut-ref="mypoint"/>
        aop:advisor advice-ref="myAround" pointcut-ref="mypoint"/>
    aop:config>

beans>

 

spring aop中aspect和advisor的区别

标签:base   bsp   inter   asp   通知   proc   contex   stat   text   

原文地址:https://www.cnblogs.com/Tony100/p/13030275.html


评论


亲,登录后才可以留言!