使用原始xml文件方式完成 spring AOP项目的搭建;完成各种通知

时间: 2024-05-14 12:12:08 浏览: 11
1. 创建项目 首先,我们需要创建一个基于Maven的Spring项目。在pom.xml文件中添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.8.RELEASE</version> </dependency> </dependencies> ``` 2. 创建目录结构 在src/main目录下创建以下目录结构: ``` src/main/java └── com └── example └── aop ├── aspect │ ├── AfterReturningAspect.java │ ├── AfterThrowingAspect.java │ ├── AroundAspect.java │ ├── BeforeAspect.java │ └── LoggingAspect.java ├── service │ ├── CustomerService.java │ └── CustomerServiceImpl.java └── MainApp.java ``` 3. 编写Java类 3.1 CustomerService接口 ```java public interface CustomerService { void addCustomer(); String addCustomerReturnValue(); void addCustomerThrowException() throws Exception; void addCustomerAround(String name); } ``` 3.2 CustomerServiceImpl实现类 ```java public class CustomerServiceImpl implements CustomerService { private String name; private String url; public void setName(String name) { this.name = name; } public void setUrl(String url) { this.url = url; } public void addCustomer() { System.out.println("addCustomer() is running "); } public String addCustomerReturnValue() { System.out.println("addCustomerReturnValue() is running "); return "abc"; } public void addCustomerThrowException() throws Exception { System.out.println("addCustomerThrowException() is running "); throw new Exception("Generic Error"); } public void addCustomerAround(String name){ System.out.println("addCustomerAround() is running, args : " + name); } } ``` 3.3 日志切面类 ```java public class LoggingAspect { public void beforeAdvice(JoinPoint joinPoint){ System.out.println("Going to setup student profile."); } public void afterAdvice(JoinPoint joinPoint){ System.out.println("Student profile has been setup."); } public void afterReturningAdvice(Object retVal){ System.out.println("Returning:" + retVal.toString() ); } public void AfterThrowingAdvice(IllegalArgumentException ex){ System.out.println("There has been an exception: " + ex.toString()); } public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("Around advice : Before method execution"); proceedingJoinPoint.proceed(); System.out.println("Around advice : After method execution"); } } ``` 3.4 AspectJ切面类 ```java public class BeforeAspect { public void beforeAdvice(JoinPoint joinPoint){ System.out.println("Before method:" + joinPoint.getSignature()); } } ``` ```java public class AfterReturningAspect { public void afterReturningAdvice(Object retVal){ System.out.println("Returning:" + retVal.toString() ); } } ``` ```java public class AfterThrowingAspect { public void AfterThrowingAdvice(IllegalArgumentException ex){ System.out.println("There has been an exception: " + ex.toString()); } } ``` ```java public class AroundAspect { public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("Around advice : Before method execution"); proceedingJoinPoint.proceed(); System.out.println("Around advice : After method execution"); } } ``` 4. 编写配置文件 4.1 applicationContext.xml ```xml <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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- bean definition --> <bean id="customerService" class="com.example.aop.service.CustomerServiceImpl"> <property name="name" value="Cnblogs" /> <property name="url" value="http://www.cnblogs.com" /> </bean> <!-- AspectJ Config --> <bean id="loggingAspect" class="com.example.aop.aspect.LoggingAspect"></bean> <bean id="beforeAspect" class="com.example.aop.aspect.BeforeAspect"></bean> <bean id="afterReturningAspect" class="com.example.aop.aspect.AfterReturningAspect"></bean> <bean id="afterThrowingAspect" class="com.example.aop.aspect.AfterThrowingAspect"></bean> <bean id="aroundAspect" class="com.example.aop.aspect.AroundAspect"></bean> <!-- AOP Config --> <aop:config> <aop:aspect id="logging" ref="loggingAspect"> <aop:pointcut expression="execution(* com.example.aop.service.*.*(..))" id="service"/> <aop:before pointcut-ref="service" method="beforeAdvice"/> <aop:after pointcut-ref="service" method="afterAdvice"/> <aop:after-returning pointcut-ref="service" returning="retVal" method="afterReturningAdvice" /> <aop:after-throwing pointcut-ref="service" throwing="ex" method="AfterThrowingAdvice" /> <aop:around pointcut-ref="service" method="aroundAdvice" /> </aop:aspect> <aop:aspect id="before" ref="beforeAspect"> <aop:pointcut expression="execution(* com.example.aop.service.*.*(..))" id="service"/> <aop:before pointcut-ref="service" method="beforeAdvice"/> </aop:aspect> <aop:aspect id="afterReturning" ref="afterReturningAspect"> <aop:pointcut expression="execution(* com.example.aop.service.*.*(..))" id="service"/> <aop:after-returning pointcut-ref="service" returning="retVal" method="afterReturningAdvice" /> </aop:aspect> <aop:aspect id="afterThrowing" ref="afterThrowingAspect"> <aop:pointcut expression="execution(* com.example.aop.service.*.*(..))" id="service"/> <aop:after-throwing pointcut-ref="service" throwing="ex" method="AfterThrowingAdvice" /> </aop:aspect> <aop:aspect id="around" ref="aroundAspect"> <aop:pointcut expression="execution(* com.example.aop.service.*.*(..))" id="service"/> <aop:around pointcut-ref="service" method="aroundAdvice" /> </aop:aspect> </aop:config> </beans> ``` 5. 编写启动类 ```java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.example.aop.service.CustomerService; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); CustomerService customerService = (CustomerService) context.getBean("customerService"); customerService.addCustomer(); System.out.println("------------------------------"); customerService.addCustomerReturnValue(); System.out.println("------------------------------"); try { customerService.addCustomerThrowException(); } catch (Exception e) { } System.out.println("------------------------------"); customerService.addCustomerAround("w3cschool.cc"); } } ``` 6. 运行结果 ``` Going to setup student profile. addCustomer() is running Student profile has been setup. ------------------------------ Going to setup student profile. addCustomerReturnValue() is running Returning:abc Student profile has been setup. ------------------------------ Going to setup student profile. addCustomerThrowException() is running There has been an exception: java.lang.Exception: Generic Error Student profile has been setup. ------------------------------ Around advice : Before method execution addCustomerAround() is running, args : w3cschool.cc Around advice : After method execution ```

相关推荐

最新推荐

recommend-type

Spring AOP + 注解实现统一注解功能

本文我们通过Spring AOP和Java的自定义注解来实现日志的插入功能,非常不错,具有一定的参考借鉴价值,需要的朋友一起看看吧
recommend-type

Spring AOP执行先后顺序实例详解

主要介绍了Spring AOP执行先后顺序实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

利用Spring AOP记录方法的执行时间

给大家介绍的是spring的aop来实现方法级的执行时间的记录监控,以此来评估方法的性能以及针对性的对已存在的方法进行优化。对于监控,我们比较关注监控的可靠性和性能,准确,高效,这才能在不影响整体性能的情况下...
recommend-type

Java之Spring AOP 实现用户权限验证

本篇文章主要介绍了Java之Spring AOP 实现用户权限验证,用户登录、权限管理这些是必不可少的业务逻辑,具有一定的参考价值,有兴趣的可以了解一下。
recommend-type

Springboot接口项目如何使用AOP记录日志

主要介绍了Springboot接口项目如何使用AOP记录日志,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。