广告
返回顶部
首页 > 资讯 > 后端开发 > Python >带你了解Spring AOP的使用详解
  • 187
分享到

带你了解Spring AOP的使用详解

2024-04-02 19:04:59 187人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录springMVC.xmlBankDaoAdminCheckBankDaoImplLogInfoTransMactionAdminCheckInterceptorLogInfoI

springmvc.xml


<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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    https://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <bean id="adminCheck" class="cn.hp.impl.AdminCheck"></bean>
    <bean id="transmaction" class="cn.hp.impl.Transmaction"></bean>
    <bean id="bankDao" class="cn.hp.impl.BankDaoImpl">
<!--        <property name="adminCheck" ref="adminCheck"></property>-->
<!--        <property name="transmaction" ref="transmaction"></property>-->
    </bean>
<!--    <aop:config>-->
<!--&lt;!&ndash;        切入点,什么时候,执行什么切入进来&ndash;&gt;-->
<!--        <aop:pointcut id="savepoint" expression="execution(* cn.hp.impl.*.*(..))"/>-->
<!--&lt;!&ndash;        切面-用来做事情-执行业务逻辑之前-你要做或执行什么事情&ndash;&gt;-->
<!--        <aop:aspect id="adminincepter" ref="adminCheck">-->
<!--            <aop:before method="check" pointcut-ref="savepoint"></aop:before>-->
<!--        </aop:aspect>-->
<!--        -->
<!--        <aop:aspect id="transmactionincepter" ref="transmaction">-->
<!--            <aop:around method="dointcepter" pointcut-ref="savepoint"></aop:around>-->
<!--        </aop:aspect>-->
<!--    </aop:config>-->
    <bean id="logInfo" class="cn.hp.impl.LogInfo"></bean>
    <bean id="adminCheckInterceptor" class="cn.hp.interceptor.AdminCheckInterceptor">
        <property name="adminCheck" ref="adminCheck"></property>
    </bean>
    <bean id="logInfoInceptor" class="cn.hp.interceptor.LogInfoInceptor">
        <property name="logInfo" ref="logInfo"></property>
    </bean>
    <bean id="transmactionInterceptor" class="cn.hp.interceptor.TransmactionInterceptor">
        <property name="transmaction" ref="transmaction"></property>
    </bean>
<!--    注入代理类-->
    <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--代理对象是谁-->
        <property name="target" ref="bankDao"></property>
<!--        代理的接口-->
        <property name="proxyInterfaces" value="cn.hp.dao.BankDao"></property>
<!--        代理的通知组件-->
        <property name="interceptorNames">
            <list>
                <value>adminCheckInterceptor</value>
                <value>logInfoInceptor</value>
                <value>transmactionInterceptor</value>
            </list>
        </property>
    </bean>



</beans>

BankDao


package cn.hp.dao;
public interface BankDao {
    
    public void remit();
    
    public void withdrawMoney();
    
    public void transferAccounts();
}

AdminCheck


package cn.hp.impl;
public class AdminCheck {
    public void check(){
        System.out.println("正在验证账号信息");
    }
}

BankDaoImpl


package cn.hp.impl;
import cn.hp.dao.BankDao;
public class BankDaoImpl implements BankDao {

    @Override
    public void remit() {
//        adminCheck.check();
//        transmaction.beginTransmaction();
        System.out.println("存钱的业务逻辑");
//        transmaction.closeTransmaction();
    }
    @Override
    public void withdrawMoney() {
//        adminCheck.check();
//        transmaction.beginTransmaction();
        System.out.println("取钱的业务逻辑");
//        transmaction.closeTransmaction();
    }
    @Override
    public void transferAccounts() {
        System.out.println("转账的业务逻辑");
    }
}

LogInfo


package cn.hp.impl;
public class LogInfo {
    public void write(){
        System.out.println("写日志中");
    }
}

Transmaction


package cn.hp.impl;
import org.aspectj.lang.ProceedingJoinPoint;
public class Transmaction {
    public void beginTransmaction(){
        System.out.println("开始事务");
    }
    public void closeTransmaction(){
        System.out.println("结束事务");
    }
    public void dointcepter(ProceedingJoinPoint point) throws Throwable {
        beginTransmaction();
        point.proceed();
        closeTransmaction();
    }
}

AdminCheckInterceptor


package cn.hp.interceptor;
import cn.hp.impl.AdminCheck;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
import java.util.Set;
public class AdminCheckInterceptor implements MethodBeforeAdvice {
    private AdminCheck adminCheck;
    public AdminCheck getAdminCheck() {
        return adminCheck;
    }
    public void setAdminCheck(AdminCheck adminCheck) {
        this.adminCheck = adminCheck;
    }
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        adminCheck.check();
    }
}

LogInfoInceptor


package cn.hp.interceptor;
import cn.hp.impl.LogInfo;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogInfoInceptor implements AfterReturningAdvice {
    private LogInfo logInfo;
    public LogInfo getLogInfo() {
        return logInfo;
    }
    public void setLogInfo(LogInfo logInfo) {
        this.logInfo = logInfo;
    }
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        logInfo.write();
    }
}

TransmactionInterceptor


package cn.hp.interceptor;
import cn.hp.impl.Transmaction;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.ibatis.transaction.Transaction;
public class TransmactionInterceptor implements MethodInterceptor {
    private Transmaction transmaction;
    public Transmaction getTransmaction() {
        return transmaction;
    }
    public void setTransmaction(Transmaction transmaction) {
        this.transmaction = transmaction;
    }
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        transmaction.beginTransmaction();
        Object obj = invocation.proceed();
        transmaction.closeTransmaction();
        return obj;
    }
}

Test


package cn.hp.test;
import cn.hp.dao.BankDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {
        //test1();
//        test2();
        test3();
    }
    private static void test3() {
        ApplicationContext ac= new ClassPathXmlApplicationContext("springmvc.xml");
        BankDao proxyFactory = ac.getBean("proxyFactory", BankDao.class);
        proxyFactory.remit();
    }
    private static void test2() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("springmvc.xml");
        BankDao bd = ac.getBean("bankDao",BankDao.class);
        bd.transferAccounts();
    }
    private static void test1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("springmvc.xml");
        BankDao bd = ac.getBean("bankDao",BankDao.class);
        bd.withdrawMoney();
    }
}

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注编程网的更多内容!

--结束END--

本文标题: 带你了解Spring AOP的使用详解

本文链接: https://www.lsjlt.com/news/130266.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 带你了解Spring AOP的使用详解
    目录springmvc.xmlBankDaoAdminCheckBankDaoImplLogInfoTransmactionAdminCheckInterceptorLogInfoI...
    99+
    2022-11-12
  • 一篇文章带你了解Spring AOP 的注解
    目录1、xml 的方式实现 AOP①、接口 UserService②、实现类 UserServiceImpl③、切面类,也就是通知类 MyAspect④、AOP配置文件 applic...
    99+
    2022-11-13
  • 一篇文章带你详解Spring的AOP
    目录1、AOP 什么?2、需求3、解决办法1:使用静态代理第一步:创建 UserService 接口第二步:创建 UserService的实现类第三步:创建事务类 MyTransac...
    99+
    2022-11-13
  • 带你了解如何使用Spring基于ProxyFactoryBean创建AOP代理
    目录1 基础2 JavaBean属性3 JDK和CGLIB代理总结若使用 Spring IoC 容器(ApplicationContext或BeanFactory)作为你的业务对象(...
    99+
    2022-11-12
  • Spring中的AOP操作你了解吗
    目录一、AOP操作术语 1. 连接点2. 切入点3. 通知(增强)4. 切面二、AOP操作2.1 切入点表达式2.2 AOP操作(AspectJ 注解方式)2.3 相同切入...
    99+
    2022-11-13
  • 带你详细了解Spring Security的注解方式开发
    目录默认情况下,不会开启注解,如果想用注解,需要开启注解支持。总结默认情况下,不会开启注解,如果想用注解,需要开启注解支持。 在启动类上开启: @EnableGlobalMethod...
    99+
    2022-11-12
  • 带你了解Spring中bean的获取
    目录Spring 中bean的获取总结Spring 中bean的获取 1.通过context.getbean 的方式来获取bean ApplicationContext:是spri...
    99+
    2022-11-12
  • 一文带你了解Spring中@Enable开头注解的使用
    目录@Enable 注解@Import 注解为什么要使用 @Import 注解呢总结@Enable 注解 首先我们先看一下有哪些常用的 @Enable 开头的注解...
    99+
    2022-11-13
  • Java的Spring AOP详细讲解
    目录什么是AOP&作用AOP的动态代理技术基于JDK的动态代理cglib动态代理AOP相关概念AOP开发明确事项需要编写的内容AOP技术实现的内容AOP 底层使用哪种代理方式...
    99+
    2022-11-13
  • 一文详解Spring AOP的配置与使用
    目录1.关于AOP2.初步使用AOP环境配置3.使用原生Spring API接口实现AOP4.使用自定义类实现AOP5.使用注解实现AOP1.关于AOP 面向切面编程(俗称AOP)提...
    99+
    2022-11-13
    Spring AOP配置 Spring AOP使用 Spring AOP
  • Redis详解(一)冰叔带你了解Redis
    Redis 是一种基于 键值对 的 NoSQL 数据库。与很多键值对数据库不同,Redis 提供了丰富的 值数据存储结构,包括 string(字符串)、hash(哈希)、list(列表)、set(集合)、zset(有序集合)、bitmap(...
    99+
    2020-02-04
    Redis详解(一)冰叔带你了解Redis
  • 一篇文章带你了解初始Spring
    目录为什么要使用SpringSpring概述Spring容器使用流程1.启动容器2.完成bean的初始化3.注册bean到容器中4.装配bean的属性bean的注册bean属性注入总...
    99+
    2022-11-12
  • 一文带你详细了解jQuery
    目录举个例子 : jQuery 使用常见插件常用的一些内容jQuery于2006年1月由John Resig在BarCamp NYC首次发布。它目前由Timmy Wilso...
    99+
    2023-05-15
    Javascript jQuery
  • 一篇文章带你详解Spring的概述
    目录1、什么是 Spring 2、Spring 起源3、Spring 特点①、方便解耦,简化开发②、AOP编程的支持③、声明式事务的支持④、方便程序的测试⑤、方便集成各种优秀框架⑥、...
    99+
    2022-11-13
  • Spring MVC项目中log4J和AOP使用详解
    前言日志处理是每个项目当中一个非常重要的内容。没有了日志,也就失去了对系统的可控性。没有日志,系统出现任何问题,都会没有踪迹可寻,这对一个信息系统而言是非常危险的。项目中需要将service中的类方法的调用过程,使用log4j日志记录。se...
    99+
    2023-05-30
    springmvc aop log4j
  • 一文带你了解async/await的使用
    目录关于 JS 中异步编程的一点历史有时 Promise 的级别太低,不适合使用使用 async/awaitasync/await 在同步和异步代码中提供了统一的体验async/aw...
    99+
    2022-11-13
  • 一文带你了解ChatGPT API的使用
    目录1.概述2.内容2.1 ChatGPT优点2.2 ChatGPT的应用场景2.3 ChatGPT的发展前景3.API应用4.API代码实现4.1 Python...
    99+
    2023-02-27
    ChatGPT API使用 ChatGPT API
  • 一文带你深入了解Node.js(图文详解)
    本篇文章通过超多代码和图解来带大家深入解析Node.js,主要内容包括模块化处理、包的基本应用、Express、跨域、操作Mysql数据库等,希望对大家有所帮助!一、Node.js简介1.1什么是Node.jsNode.js是一个调用内置A...
    99+
    2023-05-14
    nodejs
  • SpringAspectJ实现AOP的方法你了解吗
    目录1、什么是 AspectJ2、切入点表达式AOP 切入点表达式支持多种形式的定义规则:2、Aspect 通知类型3、AOP具体实例①、创建接口②、创建实现类③、创建切面类(包含各...
    99+
    2022-11-13
  • 一文带你了解Python中pymysql的使用
    目录前言一、pymysql用途二、下载1.下载依赖2.下载方式三、使用 1.连接Mysql数据库2.创建游标对象 3.执行函数4.获取查询结果集的方法前言 首先使用python很大一部分人是用于数据分...
    99+
    2023-02-21
    Python pymysql使用 Python pymysql
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作