Spring AOP(完结)

2020-02-28  本文已影响0人  拼搏男孩

一、第五种方式

1、AOP的相关概念

2、第五种方式

Student.class

package com.qianfeng.aop05;


public class Student implements Person {
    @Override
    public boolean eat() {
        System.out.println("I can eat");
        return true;
    }
    @Override
    public String drink() {
        System.out.println(1/0);
        System.out.println("I can drink");
        return null;
    }
}

beans5.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: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="st" class="com.qianfeng.aop05.Student" />
    <bean id="my" class="com.qianfeng.aop05.MyAspect" />
    <aop:config proxy-target-class="true">
        <aop:aspect ref="my">
            <aop:pointcut id="pt" expression="(execution(boolean com.qianfeng.aop05.*.*(..))) or (execution(java.lang.String com.qianfeng.aop05.*.*(..)))"/>
            <aop:around method="myAround" pointcut-ref="pt"/>
            <aop:before method="myBefore" pointcut-ref="pt"/>
            <aop:after method="myAfter" pointcut-ref="pt"/>

            <aop:after-returning method="myReturn" pointcut-ref="pt" returning="object"/>
            <aop:after-throwing method="myThrow" pointcut-ref="pt" throwing="e"/>
        </aop:aspect>
    </aop:config>
</beans>

MyAspect.java

package com.qianfeng.aop05;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

public class MyAspect{
    public void myBefore(JoinPoint jp){
        System.out.println("args:"+ Arrays.toString(jp.getArgs()));
        System.out.println("toString:"+jp.toString());
        System.out.println("getTarget:"+jp.getTarget());
        System.out.println("---------before----------");
    }
    public void myAfter(){
        System.out.println("---------after----------");
    }
    public Object myAround(ProceedingJoinPoint pjp){
        System.out.println("this is around-before");
        try {
            Object obj = pjp.proceed();
            System.out.println("this is around-after"+obj);
            return obj;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    public void myReturn(JoinPoint jp,Object object){
        System.out.println("this is after returning "+object);
    }

    public void myThrow(JoinPoint jp,Throwable e){
        System.out.println("this is after throwing "+e.getMessage());
    }
}

二、第六种方式

第五种方式使用xml文件配置较多步骤,比较麻烦,第六种方式使用注解更为简便

beans6.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:aop="http://www.springframework.org/schema/aop"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.qianfeng.aop06"/>
    <aop:aspectj-autoproxy/>
</beans>

相较于第五种方式,第六种方式主要的不同就是可以实现自动代理,xml文件的配置有以下不同:

MyAspect.java

package com.qianfeng.aop06;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;
@Component
@Aspect
public class MyAspect{
    @Pointcut(value = "execution(boolean com.qianfeng.aop06.*.*(..))")
    public void setAll(){}
    @Before(value = "execution(java.lang.String com.qianfeng.aop06.*.*(..))")
    public void myBefore(JoinPoint jp){
        System.out.println("args:"+ Arrays.toString(jp.getArgs()));
        System.out.println("toString:"+jp.toString());
        System.out.println("getTarget:"+jp.getTarget());
        System.out.println("---------before----------");
    }
    @After("setAll()")
    public void myAfter(){
        System.out.println("---------after----------");
    }
    @Around("setAll()")
    public Object myAround(ProceedingJoinPoint pjp){
        System.out.println("this is around-before");
        try {
            Object obj = pjp.proceed();
            System.out.println("this is around-after"+obj);
            return obj;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
    @AfterReturning(value = "setAll()",returning = "object")
    public void myReturn(JoinPoint jp,Object object){
        System.out.println("this is after returning "+object);
    }
    @AfterThrowing(value = "setAll()",throwing = "e")
    public void myThrow(JoinPoint jp,Throwable e){
        System.out.println("this is after throwing "+e.getMessage());
    }
}

三、第七种方式

第七种方式使用BeanPostProcessor方式实现。该接口我们也叫后置处理器,作用是在Bean对象在实例化和依赖注入完毕后,在显示调用初始化方法的前后添加我们自己的逻辑。注意是Bean实例化完毕后及依赖注入完成后触发的。

Spring中Bean的运行顺序:

beans7.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:aop="http://www.springframework.org/schema/aop"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.qianfeng.aop07"/>
    <bean class="com.qianfeng.aop07.MyBeanPostProcessor"/>
</beans>

MyBeanPostProcessor.java

package com.qianfeng.aop07;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("this is before "+bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("this is after");
        return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                MyAspect ma = new MyAspect();

                ma.before();

                Object obj = method.invoke(bean, args);

                ma.after();

                return obj;
            }
        });

    }
}

上一篇下一篇

猜你喜欢

热点阅读