Spring实现Aop的方式

前言

为什么要用Spring实现AOP?
我们不希望增加新功能的同时改变了原有的代码,因此我们使用Aop来保持原有的代码同时可以新增功能。

方式一:使用原生Spring API接口

使用

这里我们先建立几个实体类和测试类

项目目录如下所示:
在这里插入图片描述

AfterLog

package com.cjh.log;


import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {

    // returnValue:返回值
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
    }
}

Log

package com.cjh.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {

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

AfterLog和Log属于我们增加的功能,在原有的基础上面增加的。

下面是主类

UserService

package com.cjh.service;

public interface UserService {
    public void add();
    public void delate();
    public void update();
    public void select();
}

UserServicelmpl

package com.cjh.service;

public class UserServicelmpl implements UserService{
    public void add(){
        System.out.println("增加了一名用户!");
    }
    public void delate(){
        System.out.println("删除了一名用户");
    }
    public void update(){
        System.out.println("修改了一名用户");
    }
    public void select(){
        System.out.println("查找用户");
    }
}

applicationContext.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-->
    <bean id="userService" class="com.cjh.service.UserServicelmpl"/>
    <bean id="log" class="com.cjh.log.Log"/>
    <bean id="afterlog" class="com.cjh.log.AfterLog"/>


    <!--方式一:使用原生Spring API接口-->
    <!--配置aop:需要导入aop的约束-->
    <aop:config>
        <!--切入点:我们需要在哪里执行spring    expression表达式
        execution(要执行的位置!)
        -->
        <aop:pointcut id="pointcut" expression="execution(* com.cjh.service.UserServicelmpl.*(..))"/>

       <!--执行环绕增强-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

测试类

MyTest

import com.cjh.service.UserService;
import com.cjh.service.UserServicelmpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
       //动态代理代理的是接口(要注意)
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

这里要注意的是,动态代理代理的是接口,而不是一个类,如果写一个类进去代理,会报错

方式二:自定义类实现AOP

创建一个自定义的类

在这里插入图片描述

我们通过一个切入面

DiyPointCut

package com.cjh.diy;

public class DiyPointCut {

    public void before(){
        System.out.println("========方法执行前========");
    }

    public void after(){
        System.out.println("========方法执行后========");
    }
}

applicationContext.xml

<!--方式二:自定义类来实现aop-->
    <bean id="diy" class="com.cjh.diy.DiyPointCut"/>

    <aop:config>
        <!--自定义切面,ref 要引用的类-->
        <aop:aspect ref="diy">
        <!--切入点-->
        <aop:pointcut id="point" expression="execution(* com.cjh.service.UserServicelmpl.*(..))"/>
        <!--通知-->
        <aop:before method="before" pointcut-ref="point"/>
        <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>

    </aop:config>

测试

import com.cjh.service.UserService;
import com.cjh.service.UserServicelmpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
       //动态代理代理的是接口(要注意)
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

结果

在这里插入图片描述
这种方法相对来说,比较简单

方式三:注解的方法

创建一个类

AnnotationPointCut

package com.cjh.diy;


//方式三:使用注解方式来实现aop

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 AnnotationPointCut {


    //注解实现切入点
    @Before("execution(* com.cjh.service.UserServicelmpl.*(..))")
    public void before(){
        System.out.println("======方法执行前======");
    }

    @After("execution(* com.cjh.service.UserServicelmpl.*(..))")
    public void after(){
        System.out.println("======方法执行后======");
    }

    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.cjh.service.UserServicelmpl.*(..))")
    public void around(){

    }
}

applicationContext.xml

 <!--方式三 使用注解实现-->
    <bean id="annotationPointCut" class="com.cjh.diy.AnnotationPointCut"/>
    <!--开启注解支持              jdk(proxy-target-class="false"(默认)         cglib(proxy-target-class="true")-->
    <aop:aspectj-autoproxy/>

小结

用注解来实现的话代码比较简单也很明了,但是容错率比较低。

总结

AOP就是在不影响原有代码的基础上实现动态的增强

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我菜菜就好

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值