首页 > 其他分享 >AOP的实现方式一

AOP的实现方式一

时间:2023-02-21 20:12:41浏览次数:21  
标签:lostyou 方式 实现 void AOP println import public

一.什么是AOP

AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。

AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。

利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

 使用Spring接口

接口:

package top.lostyou.service;

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void query();
}

 

真实对象:

package top.lostyou.service;

public class UserServiceImp implements UserService {
    public void add() {
        System.out.println("增加了一个用户!");
    }

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

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

    public void query() {
        System.out.println("查询了一个用户");
    }
}

 

使用spring的代理后,我们可以使用一些spring的一些接口来完成动态代理,这些接口都是前辈们写好的,我们可以像套模板一样直接使用

动态代理接口:

package top.lostyou.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()+"被执行了");
    }
}

package top.lostyou.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 {
        // returnValue :返回值
        System.out.println("被执行了"+ method.getName()+"方法,返回值为:"+returnValue);
    }
}

 

spring配置bean 和 aop

<?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="userServiceImp" class="top.lostyou.service.UserServiceImp"/>
    <bean id="log" class="top.lostyou.log.Log"/>
    <bean id="afterLog" class="top.lostyou.log.AfterLog"/>

    <!-- 方式一:使用原生的spring API接口 -->
    <!-- 配置AOP:需要导入约束 -->
    <aop:config>
        <!-- 切入点  expression:表达式 , execution(要执行的位置! * * * * * * ) -->
        <aop:pointcut id="poincut" expression="execution(* top.lostyou.service.UserServiceImp.*(..))"/>
        <!-- 执行环绕增加!  -->
        <aop:advisor advice-ref="log" pointcut-ref="poincut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="poincut"/>
    </aop:config>
</beans>

 

这里要注意的是,我们所配置的切入点,有一个模板公式:

expression表达式:其中  execution(* * * * * *)

这六个  *  号的意思为:修饰符  返回值  包名.类名/接口名.方法名(参数列表)

使用是一般用  *  号占位,代表“所有”

 

测试类:

import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.lostyou.service.UserService;
import top.lostyou.service.UserServiceImp;

public class tsetMy {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 动态代理的必须是接口:重点
        UserService imp = (UserService)context.getBean("userServiceImp");
        imp.add();
    }
}

 

标签:lostyou,方式,实现,void,AOP,println,import,public
From: https://www.cnblogs.com/5ran2yl/p/17142236.html

相关文章

  • 0x04_My-OS实现自定义颜色
    前言:0x03我们提到:把12(红色)用循环写入显存,每个像素点怎么显示都要看对应的显存地址,比如0xa0000到0xaffff就是每一个像素点的显存你问为什么12就是红色,这些东西在主板出......
  • AOP
    *概念:面向切面编程  OOP:面向对象编程*作用:在不惊动原始设计的基础上为其进行功能增强*spring理念:无侵入式编程*核心概念: ......
  • SpringBoot+Lock4j实现高性能分布式锁
    1.简介  在分布式业务开发中,很多场景都需要添加分布式锁。在具体实践过程中,研发人员都需要自行实现,导致实现方式不统一,代码风格迥异,难以维护。  在Mybatis-Plus生态......
  • koa中间件的实现原理
    koa中间件的实现原理如何?先来看一个例子。koa的执行顺序是这样的:constmiddleware=asyncfunction(ctx,next){console.log(1)awaitnext()console.log(6)......
  • 记录--uni-app实现京东canvas拍照识图功能
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助最近公司出了一个新的功能模块(如下图),大提上可以描述为实现拍照完上传图片,拖动四方框拍照完成上传功能......
  • Java+Jquer实现趋势图
    这一篇主要介绍的是电商网站的统计功能,后台使用的是Java语言,springMvc框架结合前端Jquer,前端趋势展示组件使用的是百度开源框架Echarts,这个应该大家或多或少的都有了解......
  • cert manager搭配alidns-webhook实现ingress证书签发
    安装certmanager如果已经安装certmanager则不用执行这一步helminstall\cert-managerjetstack/cert-manager\--namespacecert-manager\--create-name......
  • selenium结合tenacity的retry实现验证码失败重试
    说在前面验证码登录的demo后续可以单独讲解,VIP学员对这部分应该都是掌握的,此处不再赘述本文假设了一个场景你通过OCR识别的验证码是有一定的错误几率的本文是通过识......
  • leetcode 28. 实现strStr()
    库函数#include<iostream>#include<string>usingnamespacestd;#definedebug(x)cout<<#x<<":"<<x<<endl;classSolution{public:intstrStr(stringh......
  • 如何获取spring aop代理类的被代理类或者说是获取目标类
    @RestControllerpublicclassTestController{@AutowiredprivateConsulRetryRegistryconsulRetryRegistry;@RequestMapping("server/offline")......