[原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在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的更多相关文章
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理
通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...
- [原创]java WEB学习笔记95:Hibernate 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring实战第八章学习笔记————使用Spring Web Flow
Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...
- Spring实战第五章学习笔记————构建Spring Web应用程序
Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...
- Spring 源码学习笔记10——Spring AOP
Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...
- Spring 源码学习笔记11——Spring事务
Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...
- 学习笔记:CentOS7学习之二十:shell脚本的基础
目录 学习笔记:CentOS7学习之二十:shell脚本的基础 20.1 shell 基本语法 20.1.1 什么是shell? 20.1.2 编程语言分类 20.1.3 什么是shell脚本 20. ...
随机推荐
- 弹性盒子布局flexbox
弹性盒子display:flexbox一般应用于父元素的容器上,然后对子元素来进行弹性布局 设置了flexbox的父元素不能设置具体的宽度与高度的值,而是通过子元素来设置值,父元素弹性的包裹既可 相关 ...
- CodeForces 515C. Drazil and Factorial
C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- windows下安装python科学计算环境,numpy scipy scikit ,matplotlib等
安装matplotlib: pip install matplotlib 背景: 目的:要用Python下的DBSCAN聚类算法. scikit-learn 是一个基于SciPy和Numpy的开源机器 ...
- wampserver的安装以及使用
一.点击wampserver.exe默认安装 二.安装成功之后测试 (1)测试基本配置 浏览器地址栏输入http://localhost/index.php ...
- BZOJ4563: [Haoi2016]放棋子
Description 给你一个N*N的矩阵,每行有一个障碍,数据保证任意两个障碍不在同一行,任意两个障碍不在同一列,要求你在 这个矩阵上放N枚棋子(障碍的位置不能放棋子),要求你放N个棋子也满足每行 ...
- Hello Java
用记事本或者Eclipse编写如下代码 public class JavaAPP{ public static void main(String[] args){ System ...
- Struts2基础数据校验和框架校验
一:三种校验的方式 1.用validate()方法实现数据校验 2.用execute()方法实现数据校验 3.用validateXxx()方法实现数据校验 1.用validate()方法实现数据校验 ...
- MySQL每天自动增加分区
有一个表tb_3a_huandan_detail,每天有300W左右的数据.查询太慢了,网上了解了一下,可以做表分区.由于数据较大,所以决定做定时任务每天执行存过自动进行分区. 1.在进行自动增加分区 ...
- ZK 页面间参数传递
1.execution.sendRedirect(url) 当使用方法execution.sendRedirect(url)进行页面跳转时,在url中添加参数:url?test=5: 跳转页面获取参数 ...
- vpn
https://itunes.apple.com/us/app/sonicwall-mobile-connect/id822514576?mt=12