其他分享
首页 > 其他分享> > spring5之AOP的基本操作

spring5之AOP的基本操作

作者:互联网

今天补发之前说的关于AOP的一些操作

Spring 框架一般都是基于 AspectJ 实现 AOP 操作,所以要使用AOP操作要导AOP相关依赖

 <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.5</version>
        </dependency>

        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.0</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.3.0</version>
        </dependency>
View Code

配置文件:

<?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"
       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/context http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--    开启注解的扫描-->
    <context:component-scan base-package="Spring"></context:component-scan>

<!--    开启aspect生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
View Code

首先是被增强类,里面内容随便写点

//被增强类
@Component
public class User {
    public void add() {
        System.out.println("add .....");
    }
}
View Code

然后是增强类,切入点表达式语法结构: execution([权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]) ):

//增强类
@Component
@Aspect
@Order(1)

public class Userproxy {
    //相同切入点抽取
    @Pointcut(value = "execution(* Spring.Aopanno.User.add(..))")
    public void pointdemo(){
    }

    //前置通知
    @Before(value = "pointdemo()")
    public void before(){
        System.out.println("before .........");
    }
    //最终通知
    @After(value = "execution(* Spring.Aopanno.User.add(..))")
    public void after(){
        System.out.println("after .........");
    }
    //后置通知
    @AfterReturning(value = "execution(* Spring.Aopanno.User.add(..))")
    public void afterrun(){
        System.out.println("afterrun .........");
    }
    //异常通知
    @AfterThrowing(value = "execution(* Spring.Aopanno.User.add(..))")
    public void afterthrow(){
        System.out.println("afterthrow .........");
    }
    //环绕通知
    @Around(value = "execution(* Spring.Aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("环绕之前.........");
        //被增强的方法执行
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后.........");
    }
}
View Code

测试就用user类获取spring创建的对象来测试

标签:void,System,add,AOP,println,基本操作,out,public,spring5
来源: https://www.cnblogs.com/chenghaixiang/p/16192128.html