Spring-04-AOP

302 阅读1分钟

使用

导入依赖包

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
</dependency>

配置

在beans中添加对应的aop约束

<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"
       >

实现方法

一使用Spring API实现

设置方法执行前后要添加的步骤

执行前

package log;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    //目标对象的方法,参数,目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass()+"的"+method.getName()+"被执行了");
    }
}

执行后

package log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass()+"的"+method.getName()+"被执行并返回:"+returnValue);
    }
}

配置aop

<aop:config>
    <!--                                      目标位置:execution( 修饰词 返回值 类名 方法名 参数-->
    <aop:pointcut id="pointcut" expression="execution(* server.UserserviceImpl.*(..))"/>
    <!--执行环绕增加-->
    <aop:advisor advice-ref="log" pointcut-ref="pointcut" ></aop:advisor>
    <aop:advisor advice-ref="alog" pointcut-ref="pointcut"></aop:advisor>
</aop:config>

执行测试

import org.springframework.context.support.ClassPathXmlApplicationContext;
import server.Userservice;
import server.UserserviceImpl;

public class Mytest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext cmx=new ClassPathXmlApplicationContext("bean.xml");
        //动态代理返回的是代理接口
        Userservice userservice=cmx.getBean("userimpl",Userservice.class);
        userservice.add();
    }
}

二 自定义类实现

自定义类

public class Diypointcut {
    public void before(){
        System.out.println("before exe");
    }
    public void after(){
        System.out.println("after out");
    }
}

注册自定义类

<bean id="diy" class="Diypointcut"></bean>

配置aop

<aop:config>
    <!--选择自定义类-->
    <aop:aspect ref="diy">
        <!--设置切点-->
        <aop:pointcut id="point" expression="execution(* server.UserserviceImpl.*(..))"/>
        <!--选择执行的方法和切点-->
        <aop:before method="before" pointcut-ref="point"/>
        <aop:after method="after" pointcut-ref="point"/>
    </aop:aspect>

</aop:config>

三 注解实现

开启注解支持

<bean id="anno" class="AnnoPointcut"></bean>
<aop:aspectj-autoproxy/>

使用注解

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect //标注为切面
public class AnnoPointcut {
    @Before("execution(* server.UserserviceImpl.*(..))")
    public void before(){
        System.out.println("before anno");
    }
    @After("execution(* server.UserserviceImpl.*(..))")
    public void after(){
        System.out.println("after anno");
    }
    @Around("execution(* server.UserserviceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around before");
        Object proceed = joinPoint.proceed();
        System.out.println("around after");
    }
}

img