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 ...
随机推荐
- 页面跳转不带 referrer的方法
如果页面中包含了如下 meta 标签,所有从当前页面中发起的请求将不会携带 referer: <meta name="referrer" content="neve ...
- php基础学习过程
1.基础知识 a.注释: <?php // 这是单行注释 # 这也是单行注释 /* 这是多行注释块 它横跨了 多行 */ ?> b.大小写敏感: 在 PHP 中,所有用户定义的函数.类和关 ...
- <form>(表单)标签和常用的类型
1.定义和用法 <form> 标签用于为用户输入创建 HTML 表单. 表单能够包含 input 元素,比如文本字段.复选框.单选框.提交按钮等等. 表单还可以包含 menus.texta ...
- 【DM8168学习笔记1】帮您快速入门 TI 的 Codec Engine
http://www.ti.com.cn/general/cn/docs/gencontent.tsp?contentId=61575 德州仪器半导体技术(上海)有限公司 通用DSP 技术应用工程师 ...
- maximum clique 1
maximum clique 1 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288KSpecial Judge, 64bit IO Format: % ...
- 微信小程序之组件的集合(三)
看看音乐播放组件是如何实现完成的音乐的播放的!!! 一.音乐music组件的开发 1.页面以及页面样式的开发 // music组件页面开发 <view hidden="{{hidden ...
- writing-mode,文字竖直书写,字符之间距离,单词之间距离
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- HTTP_REFERER的用法及伪造
引言 在php中,可以使用$_SERVER[‘HTTP_REFERER’]来获取HTTP_REFERER信息,关于HTTP_REFERER,php文档中的描述如下: “引导用户代理到当前页的前一页的地 ...
- Python实例 类和继承
class Base: def __init__(self): self.data = [] def add(self, x): self.data.a ...
- PHP获取真实客户端的真实IP的方法
REMOTE_ADDR 是你的客户端跟你的服务器“握手”时候的IP.如果使用了“匿名代理”,REMOTE_ADDR将显示代理服务器的IP. HTTP_CLIENT_IP 是代理服务器发送的HTTP头. ...