美文网首页Spring
SpringMVC 编程方式实现 AOP

SpringMVC 编程方式实现 AOP

作者: 当当一丢丢 | 来源:发表于2018-01-03 23:59 被阅读39次

相关

  • Spring支持5种类型的增强或通知(advice)
    • before、around、after、afterReturnning

前提

  • Intellij
  • 创建Maven archtype webapp
  • SpringMVC 项目 而非 spring boot
  • 添加 spring-webmvn dependency

案例

service 业务逻辑层
public interface Service {

    void print(String str);
    //void say(String str);
}

public class ServiceImpl implements Service {

    public void print(String str) {
        System.out.println("我是业务方法"+str);
    }
    //public void say(String str) {
    //   System.out.println("222"+str);
    //}
}

aop 定义增强业务

** before 增强 **

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by weixin:javajidi_com.
 * 方法执行前的逻辑,称前置通知,
 * 通过编程实现接口
 */
public class BeforeAdvice implements MethodBeforeAdvice{
    /**
    * method: target 方法
    * objects: target 方法需要的参数
    * o:目的所要增强的 target 对象
    **/
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("BeforeAdvice方法执行前");
        System.out.println(method.getName()+";"+o.getClass());
    }
}

round 环绕增强

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * 实现接口为 MethodInterceptor extends Advice
 */
public class RoundAdvice implements MethodInterceptor {

    //注意参数为:MethodInvocation
    //通过 它可以拿到参数信息
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("Roundadvice方法执行前");
        System.out.println(methodInvocation.getArguments()[0]);//可以获取目标方法的参数值
        Object result=methodInvocation.proceed();//调用目标对象的方法
        System.out.println("RoundAdvice方法执行完成了");
        return result;
    }
}

afterreturnning 增强

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

//实现 接口
public class AfterAdvice implements AfterReturningAdvice {

    /**
    * o: target 方法执行返回结果
    * method: target 方法
    * objects: target 方法参数
    * o1: 要增强 的 target 对象
    **/
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("AfterAdvice方法执行完成了");
        System.out.println(method.getName()+";"+o1.getClass());
    }
}

Test1

import org.springframework.aop.framework.ProxyFactory;
import spring4.Service;
import spring4.ServiceImpl;

/**
 * test1: 不通过 spring bean 配置文件
 */
public class Test {

    public static void main(String[] arg){
        
        //非 Spring上下文 控制 bean, 全部自定义(new)
        
        Service service=new ServiceImpl();//
        //使用代理工厂为目标对象创建代理,
        //我们自己的advice逻辑
        ProxyFactory proxyFactoryBean=new ProxyFactory();
        proxyFactoryBean.setTarget(service);//设置目标对象
        
        BeforeAdvice beforeAdvice = new BeforeAdvice();
        proxyFactoryBean.addAdvice(beforeAdvice);//为目标对象织入增强
        
        AfterAdvice afterAdvice = new AfterAdvice();
        proxyFactoryBean.addAdvice(afterAdvice);
        
        
        RoundAdvice roundAdvice = new RoundAdvice();
        proxyFactoryBean.addAdvice(roundAdvice);
        
        Service proxy=(Service)proxyFactoryBean.getProxy();
        proxy.print("test");
    }
}

classpath 路径下配置 spring.xml 实现

//在resources 目录 创建spring.xml文件


<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 由Spring 上下文 创建并装配 所有的对象-->
    <bean id="beforeAdvice" class="spring4.aop.BeforeAdvice"/>
    <bean id="afterAdvice" class="spring4.aop.AfterAdvice"/>
    <bean id="roundAdvice" class="spring4.aop.RoundAdvice"/>
    <bean id="service" class="spring4.ServiceImpl"/>
    <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

        <property name="interceptorNames">
            <list>
                <value>beforeAdvice</value>

                <value>afterAdvice</value>

                <value>roundAdvice</value>
            </list>
        </property>
        <property name="target" ref="service"></property>
    </bean>

</beans>

test2 通过bean 配置文件和Spring上下文

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring4.Service;

public class Test {

    public static void main(String[] arg){
//        Service service=new ServiceImpl();
//        //使用代理工厂为目标对象创建代理,并织入我们自己的advice逻辑
//        ProxyFactory proxyFactoryBean=new ProxyFactory();
//        proxyFactoryBean.setTarget(service);//设置目标对象
//        proxyFactoryBean.addAdvice(new BeforeAdvice());//为目标对象织入增强
//        proxyFactoryBean.addAdvice(new AfterAdvice());
//        proxyFactoryBean.addAdvice(new RoundAdvice());
//        Service proxy=(Service)proxyFactoryBean.getProxy();
//        proxy.print("test");
        
        //获取上下文中的bean
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring.xml");
        Service service=applicationContext.getBean("serviceProxy",Service.class);
        service.print("test");
    }
}

**运行结果-增强被织入到目标类的所有方法中 **

BeforeAdvice方法执行前
print;class spring4.ServiceImpl
Roundadvice方法执行前
test
我是业务方法test
RoundAdvice方法执行完成了
AfterAdvice方法执行完成了
print;class spring4.ServiceImpl

更细粒度-指定不同方法的增强
  • spring 通过org.springframework.aop.Pointcut接口描述切点,Pointcut由ClassFilter和MethodMatcher构成。
  • 通过ClassFilter定位到某些特定类上,通过MethodMatcher定位到某些特定方法上
  • 这样Pointcut就拥有了描述某些类的某些特定方法的能力。
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="beforeAdvice" class="spring4.aop.BeforeAdvice"/>
    <bean id="afterAdvice" class="spring4.aop.AfterAdvice"/>
    <bean id="roundAdvice" class="spring4.aop.RoundAdvice"/>
    <bean id="service" class="spring4.ServiceImpl"/>

    <!--通过正则表达定义增强具体的执行位置-->
    <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"/>
        <!--正则表达式用来表示增加添加到哪些类的哪些方法-->
        <property name="pattern" value="spring4\..*.print.*"/><!--表示应用到spring4包下所有类中的所有print开头的方法上-->
    </bean>
    <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

        <property name="interceptorNames">
            <list>
                <value>advisor</value><!--连接到具体执行位置-->

            </list>
        </property>
        <property name="target" ref="service"></property>
    </bean>

</beans>

test3
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring4.Service;

/**
 * Created by weixin:javajidi_com.
 */
public class Test {

    public static void main(String[] arg){
//        Service service=new ServiceImpl();
//        //使用代理工厂为目标对象创建代理,并织入我们自己的advice逻辑
//        ProxyFactory proxyFactoryBean=new ProxyFactory();
//        proxyFactoryBean.setTarget(service);//设置目标对象
//        proxyFactoryBean.addAdvice(new BeforeAdvice());//为目标对象织入增强
//        proxyFactoryBean.addAdvice(new AfterAdvice());
//        proxyFactoryBean.addAdvice(new RoundAdvice());
//        Service proxy=(Service)proxyFactoryBean.getProxy();
//        proxy.print("test");

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring.xml");
        //直接从上下文获取 proxy
        Service service=applicationContext.getBean("serviceProxy",Service.class);
        service.print("print");
        //添加多余验证代码,验证其不会被增强
        service.say("say");
    }
}

结果

BeforeAdvice方法执行前
print;class spring4.ServiceImpl
我是业务方法print
say:say

动态生成 proxy

  • 当有很多 目的target 类需要被增强时,需要手动配置很多 proxy
  • Spring 提供自动代理机制
  • 在内部Spring 使用 BeanPostProcessor 自动完成此任务
  • BeanPostProcessor 的实现类: 根据 某些规则 自动在容器实例化 bean时为符合规则条件的bean 生成代理
    • BeanNameAutoProxyCreator
      • 允许为一组特定配置名的Bean自动创建代理
    • DefaultAdvisorAutoProxyCreator
      • 它会对容器中所有的Advisor进行扫描,自动将这些切面应用到匹配的Bean中(即为目标Bean创建代理实例)
    • AnnotationAwareAspectjAutoProxyCreator
      • 为包含AspectJ注解的Bean自动创建代理实例
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="beforeAdvice" class="spring4.aop.BeforeAdvice"/>
    <bean id="afterAdvice" class="spring4.aop.AfterAdvice"/>
    <bean id="roundAdvice" class="spring4.aop.RoundAdvice"/>
    <bean id="service" class="spring4.ServiceImpl"/>

    <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"/>
        <!--正则表达式用来表示增加添加到哪些类的哪些方法-->
        <property name="pattern" value="spring4\..*.print.*"/><!--表示应用到spring4包下所有类中的所有print开头的方法上-->
    </bean>
    
    <!--会扫描所有容器中的advisor, 如上面定义的 advisor, 这个advisor 可以匹配到 若干个类
    若出现 advisor2, advisor3, advisor4 得写 相应个数 proxyFactoryBean
    然后自动为这些advisor要应用的bean生成代理-->
    <!--bean 即要被增强的 target 类-->
  <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

</beans>

test 4

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring4.Service;

/**
 * Created by weixin:javajidi_com.
 */
public class Test {

    public static void main(String[] arg){
//        Service service=new ServiceImpl();
//        //使用代理工厂为目标对象创建代理,并织入我们自己的advice逻辑
//        ProxyFactory proxyFactoryBean=new ProxyFactory();
//        proxyFactoryBean.setTarget(service);//设置目标对象
//        proxyFactoryBean.addAdvice(new BeforeAdvice());//为目标对象织入增强
//        proxyFactoryBean.addAdvice(new AfterAdvice());
//        proxyFactoryBean.addAdvice(new RoundAdvice());
//        Service proxy=(Service)proxyFactoryBean.getProxy();
//        proxy.print("test");

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring.xml");
        //要注意这里 bean name,可以debug,  
        //applicationContext.getBeanNamesForType(Service.class) 拿到 bean 名字
        Service service=applicationContext.getBean("service",Service.class);
        service.print("print");
        service.say("say");
    }
}

参考: https://www.jianshu.com/p/1dd6a26c881b

相关文章

  • SpringMVC 编程方式实现 AOP

    相关 Spring支持5种类型的增强或通知(advice)before、around、after、afterRet...

  • Spring框架AOP源码分析(二)

    AOP编程使用 1.注解版本实现AOP 2.XML方式实现AOP Xml实现aop编程:1) 引入jar文件 【...

  • web填坑-AOP

    AOP面向切面编程 利用Spring注解方式实现AOP功能 student.java StuInterceptor...

  • spring框架02

    1 AOP面向切面编程 1.1 什么是AOP AOP意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能...

  • Spring泛览二(AOP)

    AOP 面向切面编程,通过预编译和运行期动态代理,实现程序功能,是函数式编程的一个衍生范型 AOP实现方式 动态代...

  • Spring基于AspectJ的AOP的开发

    AspectJ实现AOP,实现方式: ——注解方式 ——XML方式 AspectJ实现AOP的方式比传统的AOP方...

  • spring笔记-AspectJ(AOP)

    AspectJ主要使用注解的方式来实现AOP,Spring AOP偏重编程的方式 1.示例接口 2.定义切面 2....

  • spring(3)动态代理事务操作与AOP

    6. AOP 6.1什么是AOP编程? 6.2 实现方式: 6.3 装饰者模式与静态代理对比: 6.4 动态代理...

  • spring aop原理

    一、aop概念 1、aop:面向切面 编程,扩展功能不是修改源代码,而是通过配置或者其他方式实现2、aop采取横向...

  • AOP基础之动态代理

    AOP即面向切面编程,实现的方式有很多,这篇文章主要介绍一下动态代理实现AOP的方式。主要从动态代理的原理进行分析...

网友评论

    本文标题:SpringMVC 编程方式实现 AOP

    本文链接:https://www.haomeiwen.com/subject/zudlnxtx.html