day39-Spring 11-Spring的AOP:基于AspectJ的XML配置方式








package cn.itcast.spring3.demo2; import org.aspectj.lang.ProceedingJoinPoint; /**
* 切面类
* @author zhongzh
*
*/
public class MyAspectXML {
public void before(){
System.out.println("前置通知......");
}
public void afterReturning(Object returnVal){
System.out.println("后置通知......"+returnVal);
}
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{//环绕通知,可以阻止目标方法的执行.
System.out.println("环绕前增强....");
Object result = proceedingJoinPoint.proceed();
System.out.println("环绕后增强....");
return result;
}
//异常通知
public void afterThrowing(Throwable e){
System.out.println("异常通知...."+e.getMessage());
}
//最终通知
public void after(){
System.out.println("最终通知.....");
}
}
package cn.itcast.spring3.demo2;
public class ProductDao {
//public void add(){
public int add(){
System.out.println("添加商品.......");
int d = 10/0;
return 100;
}
public void update(){
System.out.println("修改商品.......");
}
public void delete(){
System.out.println("删除商品.......");
}
public void find(){
System.out.println("查询商品.......");
}
}
package cn.itcast.spring3.demo2; import org.aspectj.lang.annotation.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class SpringTest2 {
@Autowired
@Qualifier("productDao")
private ProductDao productDao; @Test
public void demo1(){
productDao.add();
productDao.find();
productDao.update();
productDao.delete();
} }
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引AOP的约束了,Schema里面必须是带有AOP的. -->
<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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 自动代理是基于注解的. -->
<!-- 定义被增强的类 -->
<bean id="productDao" class="cn.itcast.spring3.demo2.ProductDao"></bean>
<!-- 定义切面 -->
<bean id="myAspectXML" class="cn.itcast.spring3.demo2.MyAspectXML"></bean>
<!-- 定义aop配置 --><!-- 现在已经引入了AOP的名字空间 -->
<aop:config>
<!-- 定义切点,切点其实就是在哪些类哪些方法上应用增强 -->
<aop:pointcut expression="execution(* cn.itcast.spring3.demo2.ProductDao.add(..))" id="mypointcut"/>
<!-- 应用哪一个增强 advisor和aspect都是切面 advisor是一个切点和一个通知的组合 aspect是多个切点和多个通知的组合 -->
<aop:aspect ref="myAspectXML"><!-- 定义切面 -->
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="mypointcut"/><!-- 前置通知 --><!-- 在哪个切点上面去应用前置通知 -->
<!-- pointcut-ref对切点的引用,是前置的还是后置的 -->
<!-- 后置通知 后置通知可以获取目标方法的返回值-->
<aop:after-returning method="afterReturning" pointcut-ref="mypointcut" returning="returnVal"></aop:after-returning><!-- pointcut-ref引入切点 -->
<!-- 环绕通知 -->
<aop:around method="around" pointcut-ref="mypointcut"></aop:around>
<!-- 异常通知 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="mypointcut" throwing="e"></aop:after-throwing>
<!-- 最终通知 -->
<aop:after method="after" pointcut-ref="mypointcut"></aop:after>
</aop:aspect>
</aop:config>
</beans>
day39-Spring 11-Spring的AOP:基于AspectJ的XML配置方式的更多相关文章
- spring-第十七篇之spring AOP基于注解的零配置方式
1.基于注解的零配置方式 Aspect允许使用注解定义切面.切入点和增强处理,spring框架可以识别并根据这些注解来生成AOP代理.spring只是用了和AspectJ 5一样的注解,但并没有使用A ...
- Spring MVC 中使用AOP 进行事务管理--XML配置实现
1.今天写一篇使用AOP进行事务管理的示例,关于事务首先需要了解以下几点 (1)事务的特性 原子性(Atomicity):事务是一个原子操作,由一系列动作组成.事务的原子性确保动作要么全部完成,要么完 ...
- Spring AOP基于注解的“零配置”方式实现
为了在Spring中启动@AspectJ支持,需要在类加载路径下新增两个AspectJ库:aspectjweaver.jar和aspectjrt.jar.除此之外,Spring AOP还需要依赖一个a ...
- 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)
[AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...
- 8 -- 深入使用Spring -- 4...5 AOP代理:基于注解的“零配置”方式
8.4.5 基于注解的“零配置”方式 AspectJ允许使用注解定义切面.切入点和增强处理,而Spring框架则可识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5 一样的 ...
- 【学习】Spring 的 AOP :基于Annotation 的“零配置”方式
转自:http://www.cnblogs.com/jbelial/archive/2012/07/20/2539123.html AOP(Aspect Orient Programming ) , ...
- spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)
1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...
- 基于AspectJ的XML方式进行AOP开发
-------------------siwuxie095 基于 AspectJ 的 XML 方式进行 AOP 开发 1 ...
- Spring之AOP原理、代码、使用详解(XML配置方式)
Spring 的两大核心,一是IOC,另一个是AOP,本博客从原理.AOP代码以及AOP使用三个方向来讲AOP.先给出一张AOP相关的结构图,可以放大查看. 一.Spring AOP 接口设计 1.P ...
随机推荐
- 浏览器标准模式与怪异模式-CSS1Compat and BackCompat
由于历史的原因,各个浏览器在对页面的渲染上存在差异,甚至同一浏览器在不同版本中,对页面的渲染也不同.在W3C标准出台以前,浏览器在对页面的渲染上没有统一规范,产生了差异(Quirks mode或者称为 ...
- PHP+Ajax点击加载更多内容 -这个效果好,速度快,只能点击更多加载,不能滚动自动加载
这个效果好,速度快,只能点击更多加载,不能滚动自动加载 一.HTML部分 <div id="more"> <div class="single_item ...
- JEECMS 系统权限设计
1.用户校验. 登录校验主要围绕着用户后台登陆的url拦截 a.围绕着用户登录过程中设计到两张用户表 jc_user:存储着用户的基本信息 jo_user:存储着用户登录.注册.更新时间及用户密码信息 ...
- Redis学习01——介绍与搭建环境
Redis学习01介绍与搭建环境一简介二yum配置已经配置了的可以跳过三安装gcc四安装redis1 上传Redis到linux中2 解压到usrlocal下3 编译redis4 安装redis5 拷 ...
- Leetcode448.Find All Numbers Disappeared in an Array找到所有数组中消失的数字
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能在不 ...
- 【笔记】LR中设置检查点
我们为什么需要在LR中设置检查点?? 我们在录制编写脚本后,通常会进行回放,如果回放通过没有错误.我们就认为脚本是正确的.那么LR怎么区分脚本是否回放正确:基本上所有脚本回放错误都是因为 404错 ...
- linux操作系统清除报错Disk /dev/mapper/ddf1_4c53492....
现象描述 Disk /dev/mapper/ddf1_4c5349202020202010000055000000004711471100000a28p1: 666.0 GB, 66600088934 ...
- html中有序列表标签ol,li的高级应用
本文主要介绍html中有序列表标签ol,li的高级应用, 在网页设计时我们设计有序列表内容时,经常会在每个ITEM前手工加上一个数值,或是由程序加上这个数值. 而如果使用有序列表标签ol和li,则不需 ...
- 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes【取回文数/数论/字符串】
题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...
- java图形验证码生成工具类及web页面校验验证码
最近做验证码,参考网上案例,发现有不少问题,特意进行了修改和完善. 验证码生成器: import javax.imageio.ImageIO; import java.awt.*; import ja ...