spring中Aop实现方式(附代码)

方式1:使用我们SpringAPI接口:
代码实现:

我们首先定义一个接口

public interface service {

    public void add();
    public void update();
    public void delete();
    public void select();
}

生成一个接口的实现类:

public class serviceImpl implements service {
    public void add() {
        System.out.println("增加了");
    }

    public void update() {
        System.out.println("更新了");
    }

    public void delete() {
        System.out.println("删除了");
    }

    public void select() {
        System.out.println("查找了");
    }
}

由于我们的切面(切入的过程)分为几种:before、after…在此我们只实现before来进行实现:
定义一个类实现我们的MethodBeforeAdvice

public class beforeAdvice implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("方法执行之前!!!");
    }
}

在实现的方法中就是我们代码的加强部分(底层原理就是使用我们的动态代理)

接着我们就在我们的配置文件中进行配置

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

<-以上配置我们的Aop域和约束->


<bean id="before" class="com.ks.log.beforeAdvice"></bean>


<bean id="serviceImpl" class="com.ks.serviceImpl"></bean>


    <!--方式1-->
    <aop:config>
    <-配置切入点,其中的expression就是我们的切入点(即代码需要加强的方法)->
        <aop:pointcut id="pointcut" expression="execution(* com.ks.serviceImpl.*(..))"/>
    <-配置我们的通知(即我们想要的加强)->
        <aop:advisor advice-ref="before" pointcut-ref="pointcut"/>

    </aop:config>

</beans>

(在第一种过程我出现ClassNotFoundException: org.springframework.core.NativeDetector错误,我们只需直接添加相关的额依赖即可。可能我哪里给删掉了)

方法二:自定义:

我们与方法一差不多,不过更方便
我们先自定义一个类,里面的方法就是我们的通知(增强的代码)

public class defAdvices {

    public void method01(){

        System.out.println(
                "自定义方法01"
        );
    }

    public void method02(){
        System.out.println(
                "自定义方法02"
        );
    }

}

其次我们在配置文件中添加:

<-bean的注入->
<bean id="def" class="com.ks.defAdvices"></bean>

<-aop的配置->
    <aop:config>
    <-切面,即我们的加强的类,这个类包含我们通知的类->
        <aop:aspect ref="def">
            
            <aop:pointcut id="point" expression="execution(* com.ks.serviceImpl.*(..))"/>
            <-我们通过aop:XX:XX表示我们加强的位置->
            <aop:before method="method01" pointcut-ref="point"/>
            <aop:after method="method02" pointcut-ref="point"></aop:after>
            
        </aop:aspect>

    </aop:config>

显而易见方法二比方法一简单

附:execution表达式:
在这里插入图片描述

方式三:使用注解
首先我们也是先创建一个类作为切面:

**使用Aspect将标注我们的这个类为切面**
@Aspect
public class annotationPointcut {
**使用@before表示我们作为execution表达式(切点)方法之前的**
    @Before("execution(* com.ks.serviceImpl.*(..))")
    public void before(){
        System.out.println("之前01");
    }

    @After("execution(* com.ks.serviceImpl.*(..))")
    public void after(){

        System.out.println("之后02");
    }
**使用环绕,注意我们的环绕将之前和之后的包括在内**
    @Around("execution(* com.ks.serviceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前");

        Signature signature = joinPoint.getSignature();//获得签名
        System.out.println("signature" + signature);
**执行我们的切点,如果没有执行这个将不会执行我们的切点**
        Object proceed = joinPoint.proceed();


        System.out.println("环绕后");

        System.out.println(proceed);
    }

}

其次在Xml文件中配置:


    <bean id="annotation" class="com.ks.annotationPointcut"/>

    <!--开启我们的aop注解支持-->
    <aop:aspectj-autoproxy/>

版权声明:本文为ebdbbd原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。