对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

相关文章

Java使用WebView实现桌面程序的技术指南

《Java使用WebView实现桌面程序的技术指南》在现代软件开发中,许多应用需要在桌面程序中嵌入Web页面,例如,你可能需要在Java桌面应用中嵌入一部分Web前端,或者加载一个HTML5界面以增强... 目录1、简述2、WebView 特点3、搭建 WebView 示例3.1 添加 JavaFX 依赖3

防止SpringBoot程序崩溃的几种方式汇总

《防止SpringBoot程序崩溃的几种方式汇总》本文总结了8种防止SpringBoot程序崩溃的方法,包括全局异常处理、try-catch、断路器、资源限制、监控、优雅停机、健康检查和数据库连接池配... 目录1. 全局异常处理2. 使用 try-catch 捕获异常3. 使用断路器4. 设置最大内存和线

Java Jackson核心注解使用详解

《JavaJackson核心注解使用详解》:本文主要介绍JavaJackson核心注解的使用,​​Jackson核心注解​​用于控制Java对象与JSON之间的序列化、反序列化行为,简化字段映射... 目录前言一、@jsonProperty-指定JSON字段名二、@JsonIgnore-忽略字段三、@Jso

Spring Validation中9个数据校验工具使用指南

《SpringValidation中9个数据校验工具使用指南》SpringValidation作为Spring生态系统的重要组成部分,提供了一套强大而灵活的数据校验机制,本文给大家介绍了Spring... 目录1. Bean Validation基础注解常用注解示例在控制器中应用2. 自定义约束验证器定义自

Java对接Dify API接口的完整流程

《Java对接DifyAPI接口的完整流程》Dify是一款AI应用开发平台,提供多种自然语言处理能力,通过调用Dify开放API,开发者可以快速集成智能对话、文本生成等功能到自己的Java应用中,本... 目录Java对接Dify API接口完整指南一、Dify API简介二、准备工作三、基础对接实现1.

9个SpringBoot中的自带实用过滤器使用详解

《9个SpringBoot中的自带实用过滤器使用详解》在SpringBoot应用中,过滤器(Filter)是处理HTTP请求和响应的重要组件,SpringBoot自带了许多实用的过滤器,如字符编码,跨... 目录1. CharacterEncodingFilter - 字符编码过滤器功能和配置手动配置示例2

Spring Boot Controller处理HTTP请求体的方法

《SpringBootController处理HTTP请求体的方法》SpringBoot提供了强大的机制来处理不同Content-Type​的HTTP请求体,这主要依赖于HttpMessageCo... 目录一、核心机制:HttpMessageConverter​二、按Content-Type​处理详解1.

Spring Boot 常用注解详解与使用最佳实践建议

《SpringBoot常用注解详解与使用最佳实践建议》:本文主要介绍SpringBoot常用注解详解与使用最佳实践建议,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、核心启动注解1. @SpringBootApplication2. @EnableAutoConfi

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ