对Spring的AOP的浅薄的理解

2023-10-21 09:48
文章标签 java spring 理解 aop 浅薄

本文主要是介绍对Spring的AOP的浅薄的理解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

今天重新看了 动态代理模式,又看了一遍Spring  AOP,把我的项目慢慢的添加Spring,把我了解的AOP和大家分享一下。


下面的例子参考了别人的代码

添加Spring3.0  需要的包


 

定义一个接口 HelloInterface

package rw.hello;public interface HelloInterface {void BeforeHello();void ExecuteHello();void AfterHello(); 
}

实现这个接口的类 Hello

package rw.hello;public class Hello implements HelloInterface{public void BeforeHello() {// TODO Auto-generated method stubSystem.out.println("----------------Hello 方法执行之前");}public void ExecuteHello() {// TODO Auto-generated method stubSystem.out.println("----------------Hello 方法执行中......");}public void AfterHello() {// TODO Auto-generated method stubSystem.out.println("----------------Hello 方法执行之后");}}

现在想在 Hello 类的
BeforeHello(),ExecuteHello(),AfterHello()这三个方法执行之前做其它事情,比如权限问题,记录日志之类.....反正我今天是添加的用户权限问题同样有3个类,风别是BeforeHello(),ExecuteHello(),AfterHello()这三个方法调用之前要调用的
下面只测试一下After.java 和 Before.javaAfter.javapackage rw.method;import org.aspectj.lang.JoinPoint;public class After {public void invoke(JoinPoint joinpoint){System.out.println("After 类"+joinpoint.getSignature().getName());}
}Before.javapackage rw.method;import org.aspectj.lang.JoinPoint;public class Before {public void invoke(JoinPoint joinpoint){System.out.println("Before 类"+joinpoint.getSignature().getName());}
}
最注意的还是 applicationContext.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springsource.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><bean id="hello" class="rw.hello.Hello"/><bean id="after" class="rw.method.After"/>
<bean id="before" class="rw.method.Before"/>
<bean id="execute" class="rw.method.Execute"/><aop:config>
<aop:pointcut expression="execution(* rw.hello.HelloInterface.BeforeHello(..))" id="beforepointcut"/>
<aop:aspect id="beforeaspect" ref="before"><aop:before method="invoke" pointcut-ref="beforepointcut"/>
</aop:aspect>
</aop:config><aop:config><aop:pointcut expression="execution(* rw.hello.HelloInterface.AfterHello(..))" id="afterpointcut"/><aop:aspect id="afteraspact" ref="after"><aop:after method="invoke" pointcut-ref="afterpointcut"/></aop:aspect>
</aop:config>
</beans>
排版不是很好

1,在Hello的BeforeHello()方法执行之前 会调用Before类中的invoke(JoinPoint joinpoint)方法

2,在Hello的AfterHello()方法执行之后 会调用After类中的invoke(JoinPoint joinpoint)方法

测试类

package rw.test;import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import rw.hello.HelloInterface;public class AllTest {private ApplicationContext context;@Beforepublic void setUp() throws Exception {context=new ClassPathXmlApplicationContext("applicationContext.xml");}@Testpublic void TestHelloInterface(){HelloInterface hello=(HelloInterface) context.getBean("hello");hello.BeforeHello();hello.AfterHello();}
}

输出结果

Hello 类中的 void ExecuteHello()方法在applicationContext.xml中没有加以任何配置

正常输出



这篇关于对Spring的AOP的浅薄的理解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/253571

相关文章

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试