Spring_基于配置文件的方式配置AOP
applicationContext-xml.xml
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--配置bean -->
<bean id="arithmeticCalculator" class="com.aff.spring.aop.imlp.xml.ArithmeticCalculatorImpl"></bean> <!--配置切面的bean -->
<bean id="loggingAspect" class="com.aff.spring.aop.imlp.xml.LoggingAspect"></bean> <!-- 配置AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut
expression="execution(* com.aff.spring.aop.imlp.xml.ArithmeticCalculator.*(..))"
id="pointcut" />
<!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<!--
<aop:around method="aroundMethod" pointcut-ref="pointcut"/>
-->
</aop:aspect> </aop:config>
</beans>
LoggingAspect.java
package com.aff.spring.aop.imlp.xml; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; //把这个类声明为切面,需要把这个类放到IOC容器中, 再声明为一个切面
public class LoggingAspect { /**
* 定义一个方法, 用于声明切入点表达式,一般,该方法中不再需要添加其他代码
*
* @Pointut 声明切点表达式
* 后面的其他通知 直接使用方法名来引用当前的切点表达式
*/
public void declareJointPointExpression(){ } //声明该方法是一个前置通知, 在目标方法开始之前执行
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " +methodName+"begins with"+args);
}
//后置通知:在目标方法执行后(无论 是否发生异常) ,执行的通知
//在后置通知中还不能访问目标方法执行的结果
//第一个*:任意返回值类型; 第二个*:包下的任意类; 第三个*:任意方法; .. : 任意参数
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " +methodName+"ends ");
} /**
* 返回通知
* 在方法正常结束后执行的代码
* 返回通知是可以访问到方法的返回值
* @param joinPoint
*/
public void afterReturning(JoinPoint joinPoint,Object result ){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " +methodName+"ends with "+result);
} /**
* 异常通知
* 在目标方法出现异常时 会执行的代码
* 可以访问到异常对象,且可以指定出现特定异常时在执行通知代码
* @param joinPoint
* @param ex
*/
public void afterThrowing(JoinPoint joinPoint,Exception e){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " +methodName+"ocurs exception :"+e);
} /**
* 转绕通知需要携带 ProceedingJoinPoint 类型的参数
* 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法
* 且环绕通知必须有返回值
* @param pjd
*/ public Object aroundMethod(ProceedingJoinPoint pjd){
Object result =null;
String methodName= pjd.getSignature().getName();
//执行目标方法
try {
//前置通知
System.out.println("The method " +methodName+"begins with"+Arrays.asList(pjd.getArgs())); //执行目标方法
result = pjd.proceed(); //返回通知
System.out.println("The method "+methodName +"end with"+result);
} catch (Throwable e) {
//异常通知
System.out.println("The method"+methodName+" ocurs exception :"+e);
throw new RuntimeException(e);
}
System.out.println("The method "+methodName +"ends");
return result;
} }
Main
package com.aff.spring.aop.imlp.xml; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml"); ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class); int result = arithmeticCalculator.add(2, 3);
System.out.println("result:"+result); int result2 = arithmeticCalculator.div(2, 1);
System.out.println("result2:"+result2); }
}
运行如下
The method addbegins with[2, 3]
The method addend with5
The method addends
result:5
The method divbegins with[2, 1]
The method divend with2
The method divends
result2:2
目录

Spring_基于配置文件的方式配置AOP的更多相关文章
- 基于配置文件的方式配置AOP
之前说的都是通过注释的方式配置,接下来说说如何使用配置文件配置AOP 还是原来的代码,去掉所有注释,接下来配置最基本的几个bean. 然后使用<aop:config>标签进行配置,然后配切 ...
- 22Spring基于配置文件的方式配置AOP
直接看代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalculator { int add(in ...
- spring-AOP框架(基于配置文件的方式配置AOP)
.xml: ref-指向,order-指定优先级
- Spring(二十):Spring AOP(四):基于配置文件的方式来配置 AOP
基于配置文件的方式来配置 AOP 前边三个章节<Spring(十七):Spring AOP(一):简介>.<Spring(十八):Spring AOP(二):通知(前置.后置.返回. ...
- Spring4学习笔记-AOP(基于配置文件的方式)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://shamrock.blog.51cto.com/2079212/1557743 引 ...
- [原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 详解AOP——用配置文件的方式实现AOP
AOP概念 1.AOP:面向切面(方面)编程,扩展功能不修改源代码实现 AOP原理 AOP采用横向抽取机制,取代了传统纵向继承体系重复性代码 传统的纵向抽取机制: 横向抽取机制: AOP操作术语 1. ...
- 使用Spring框架入门四:基于注解的方式的AOP的使用
一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...
- xml的方式配置AOP:Aspect Oriented Programming
在某些类中, 什么时机, 做什么事情 切入点(point-cut): 在某些类中(Class<?>[] itfc = new Class<?>[] { IStudentServ ...
随机推荐
- RobotFrameWork 自动化环境搭建(基于 python3.6)
一.需要安装的工具目录 安装 python3.6.0 我这里采用的是 python3.6 (目前慢慢市场在推广 python3.0 版本了) 安装 wxPython(Python 非常有名的一个GUI ...
- docker批量删除本地镜像和容器
长时间运行docker,每次只用docker kill去停止容器,但是从没删除过本地镜像,导致有上百个镜像在占用内存. 1.批量停止容器 docker container stop $(docker ...
- F - Power Network POJ - 1459
题目链接:https://vjudge.net/contest/299467#problem/F 这个是一个很简单的题目,但是读入很有意思,通过这个题目,我学会了一种新的读入方式. 一个旧的是(%d, ...
- ssh框架整合时的延迟加载问题(no session问题)的分析以及解决方案
当我们整合完三大框架,并采用hibernate的延迟加载方案时,会出现如下的异常: 现在对这个异常进行分析,如下图所示(模拟通过id查询用户信息的过程): 上图分析了为什么会出现no session的 ...
- C++中的隐式转换和explicit
隐式转换 c++中的数据类型转换分为隐式转换和显示转换: 显示转换即使用static_cast等方法进行转换,相关内容请参考 <C++数据类型转换>: 隐式转换则是编译器完成的,如,boo ...
- 【Hadoop离线基础总结】通过Java代码执行Shell命令
通过Java代码执行Shell命令 需求 在实际工作中,总会有些时候需要我们通过java代码通过远程连接去linux服务器上面执行一些shell命令,包括一些集群的状态管理,执行任务,集群的可视化界面 ...
- MYSQL连接端口被占引入文件路径错误
今天早上打算本地win7机器上用Wampserver搭建PHP环境,把自己的程序配置上去.下载最新的wampserver 3.0.6版本安装后,发现mysql一直无法运行,以为是wampserver版 ...
- 装完B就跑,这几个Linux指令真的Diǎo
本文介绍一些有趣的指令,实用或者可以装逼,不妨自己也来试试看: 文章目录 1 故事的开局 2 杰哥的表演 2.1 sl 2.2 htop 2.3 gcp 2.4 hollywood 2.5 cmatr ...
- HMM-前向后向算法与实现
目录 基本要素 HMM三大问题 概率计算问题 前向算法 后向算法 前向-后向算法 基本要素 状态 \(N\)个 状态序列 \(S = s_1,s_2,...\) 观测序列 \(O=O_1,O_2,.. ...
- Js调用Android回调处理
通常在混合app中经常会使用js调用native的方法,一般是: window.nativeApp.call(XXX); 直接调用native方法,对于简单的处理倒是可以,如果需要回调呢?期待的方式是 ...