本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.用基于 XML 的配置声明切面

  1) 除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的.

  2) 正常情况下, 基于注解的声明要优先于基于 XML 的声明. 通过 AspectJ 注解, 切面可以与 AspectJ 兼容, 而基于 XML 的配置则是 Spring 专有的. 由于 AspectJ 得到越来越多的 AOP 框架支持, 所以以注解风格编写的切面将会有更多重用的机会.

   

  基于 XML ---- 声明切面

  3) 当使用 XML 声明切面时, 需要在 <beans> 根元素中导入 aop Schema

  4) 在 Bean 配置文件中, 所有的 Spring AOP 配置都必须定义在 <aop:config> 元素内部. 对于每个切面而言, 都要创建一个 <aop:aspect> 元素来为具体的切面实现引用后端 Bean 实例.

  5) 切面 Bean 必须有一个标示符, 供 <aop:aspect> 元素引用

  

  基于 XML ---- 声明切入点

  6) 切入点使用 <aop:pointcut> 元素声明

  7) 切入点必须定义在 <aop:aspect> 元素下, 或者直接定义在 <aop:config> 元素下:  定义在 <aop:aspect> 元素下: 只对当前切面有效 定义在 <aop:config> 元素下: 对所有切面都有效

  8)  基于 XML 的 AOP 配置不允许在切入点表达式中用名称引用其他切入点.

  

  基于 XML ---- 声明通知

  9) 在 aop Schema 中, 每种通知类型都对应一个特定的 XML 元素.

  10)通知元素需要使用 <pointcut-ref> 来引用切入点, 或用 <pointcut> 直接嵌入切入点表达式. method 属性指定切面类中通知方法的名称.

2.关于配置的核心代码

  aop-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"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置bean -->
<bean id="arithmeticCaculator" class="com.jason.spring.aop.impl.ArithmeticCaculatorImpl"> </bean> <!-- 配置切面的bean -->
<bean id="loggingAspect" class="com.jason.spring.aop.impl.LoggingAspect"></bean>
<bean id="validateArgs" class="com.jason.spring.aop.impl.ValidateArgs"></bean> <!-- 配置aop -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.jason.spring.aop.impl.ArithmeticCaculator.*(..))" id="pointcut"/> <!-- 配置切面和通知 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
</aop:aspect>
<aop:aspect ref="validateArgs" order="1">
<aop:before method="validateArgs" pointcut-ref="pointcut"/>
</aop:aspect> </aop:config> </beans>

  

3.其他代码

  

ArithmeticCaculator.java
 package com.jason.spring.aop.impl;

 public interface ArithmeticCaculator {

     int add(int i, int j);
int sub(int i, int j); int mul(int i, int j);
int div(int i, int j); }
ArithmeticCaculatorImpl.java
 package com.jason.spring.aop.impl;

 import org.springframework.stereotype.Component;

 //@Component

 public class ArithmeticCaculatorImpl implements ArithmeticCaculator {

     @Override
public int add(int i, int j) {
int result = i + j;
return result;
} @Override
public int sub(int i, int j) {
int result = i - j;
return result;
} @Override
public int mul(int i, int j) {
int result = i * j;
return result;
} @Override
public int div(int i, int j) {
int result = i / j;
return result;
} }
LoggingAspect.java
 package com.jason.spring.aop.impl;

 import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /*@Order(1)
//把这个类声明为一个切面
//1.需要将该类放入到IOC 容器中
@Component
//2.再声明为一个切面
@Aspect*/
public class LoggingAspect { /**
*
* @Author:jason_zhangz@163.com
* @Title: declareJointPointExpression
* @Time:2016年12月6日
* @Description: 定义一个方法,用于声明切入点表达式。一般的,该方法不需要添加其他代码
*
*/
//@Pointcut("execution(* com.jason.spring.aop.impl.*.*(int, int))")
public void declareJointPointExpression(){} //声明该方法是一个前置通知:在目标方法开始之前执行 哪些类,哪些方法
//作用:@before 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,aop框架会自动的为那个方法所在的类生成一个代理对象,在目标方法执行之前,执行注解的方法
//支持通配符
//@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")
//@Before("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 " + args);
} /**
*
* @Author:jason_zhangz@163.com
* @Title: afterMethod
* @Time:2016年12月1日
* @Description: 在方法执行后执行的代码,无论该方法是否出现异常
*
* @param joinPoint
*/
//@After("declareJointPointExpression()")
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " end " + args);
} /**
*
* @Author:jason_zhangz@163.com
* @Title: afterReturning
* @Time:2016年12月1日
* @Description: 在方法正常结束后执行代码,放回通知是可以访问到方法的返回值
*
* @param joinPoint
*/
//@AfterReturning( value="declareJointPointExpression()", returning="result")
public void afterReturning(JoinPoint joinPoint ,Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " end with " + result);
} /**
*
* @Author:jason_zhangz@163.com
* @Title: afterThrowting
* @Time:2016年12月1日
* @Description: 在目标方法出现异常时会执行代码,可以访问到异常对象,且,可以指定出现特定异常时执行通知代码
*
* @param joinPoint
* @param ex
*/
//@AfterThrowing(value="declareJointPointExpression()",throwing="ex")
public void afterThrowting(JoinPoint joinPoint, Exception ex){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " occurs exceptions " + ex);
} /**
*
* @Author:jason_zhangz@163.com
* @Title: around
* @Time:2016年12月1日
* @Description: 环绕通知需要携带 ProceedingJoinPoint 类型的参数
* 环绕通知 类似于 动态代理的全过程
* ProceedingJoinPoint:可以决定是否执行目标方法
* 环绕通知必须有返回值,返回值即为目标方法的返回值
*
* @param proceedingJoinPoint
*/
//@Around("declareJointPointExpression()")
public Object around(ProceedingJoinPoint proceedingJoinPoint){ Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName(); //执行目标方法
try {
//前置通知
System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs())); result = proceedingJoinPoint.proceed(); //后置通知
System.out.println("The method " + methodName + "end with" + result); } catch (Throwable e) {
//异常通知
System.out.println("The method occurs exception : " + e);
throw new RuntimeException();
}
//后置通知 System.out.println("The method " + methodName + "end with" + result); return result; } }
ValidateArgs.java
 package com.jason.spring.aop.impl;

 import java.util.Arrays;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
*
* @ClassName:ValidateArgs
* @Description:可以使用@Order 注解指定切面的优先级,值越小优先级越高
* @author: jason_zhangz@163.com
* @date:2016年12月6日下午2:14:55
*
*/
/*@Order(2)
@Component
@Aspect*/
public class ValidateArgs { //@Before("execution(* com.jason.spring.aop.impl.ArithmeticCaculator.*(..))")
public void validateArgs(JoinPoint joinPoint){
System.out.println("validate:" + Arrays.asList(joinPoint.getArgs()));
} }
Main.java
 package com.jason.spring.aop.impl;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1.创建Spring 的IOC 容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("aop-xml.xml"); //2.从IOC 容器中获取 bean实例
ArithmeticCaculator arithmeticCaculator = (ArithmeticCaculator) ctx.getBean(ArithmeticCaculator.class); //3.使用bean
int result = arithmeticCaculator.add(1, 2);
System.out.println(result); result = arithmeticCaculator.div(1, 2);
System.out.println(result); } }

[原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP的更多相关文章

  1. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理

    通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...

  2. [原创]java WEB学习笔记95:Hibernate 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Spring实战第八章学习笔记————使用Spring Web Flow

    Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...

  6. Spring实战第五章学习笔记————构建Spring Web应用程序

    Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...

  7. Spring 源码学习笔记10——Spring AOP

    Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...

  8. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  9. 学习笔记:CentOS7学习之二十:shell脚本的基础

    目录 学习笔记:CentOS7学习之二十:shell脚本的基础 20.1 shell 基本语法 20.1.1 什么是shell? 20.1.2 编程语言分类 20.1.3 什么是shell脚本 20. ...

随机推荐

  1. QuickTest Professionar 快捷键

    新建Test – New Test CTRL + N录制 – Record F3打开Test – Open Test CTRL + O新建业务组件 – Business Component > ...

  2. java基础-包

    浏览以下内容前,请点击并阅读 声明 为了使类型更容易查找和使用,避免命名冲突,以及可视范围的控制,程序员一般将相关的一些类型组合到一个包中.组合的类型包括类,接口,枚举和注释,枚举是一种特殊的类,而注 ...

  3. 洛谷 P2737 [USACO4.1]麦香牛块Beef McNuggets Label:一点点数论 && 背包

    题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如 ...

  4. 【JAVA】Runtime

    1.内存管理:Java提供了无用单元自动收集机制.通过totalMemory()和freeMemory()方法可以知道对象的堆内存有多大,还剩多少.Java 会周期性的回收垃圾对象(未使用的对象),以 ...

  5. springboot与shiro配置

    详情参考:  http://blog.csdn.net/catoop/article/details/50520958 http://blog.csdn.net/qiuqiupeng/article/ ...

  6. BOM,文档宽高及窗口事件小析

    (一)BOM:Browser Object Model(浏览器对象模型)页面上所有的变量都是window的属性 一.方法:1. open(,)打开一个新窗口(页面)一参为页面地址url,二参为打开方式 ...

  7. *HDU1325 并查集

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. c#向数据库插入较大数据(SqlBulkCopy)

    因为要向数据库添加一些数据,数据量较大 1.使用sql语句批量提交速度较慢 2.用事物批量提交,速度一般 3.用SqlBulkCopy方法写入数据,速度较快 /// <summary> / ...

  9. 局域网内利用gitlab,jenkins自动生成gitbook并发布(nginx)

    安装了GitBook,内网使用,没法用上gitbook的网页. 用gitbook serve只能展示一本书,而且也不利于长期维护. 于是使用gitlab,jenkins,和nginx配合gitbook ...

  10. C++标准库 -- pair

    头文件:<utility> 可访问属性: first 第一个值 second 第二个值 可访问方法: swap(pair) 和另外一个pair交换值 其他相关方法: make_pair(v ...