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配置方式的更多相关文章

  1. spring-第十七篇之spring AOP基于注解的零配置方式

    1.基于注解的零配置方式 Aspect允许使用注解定义切面.切入点和增强处理,spring框架可以识别并根据这些注解来生成AOP代理.spring只是用了和AspectJ 5一样的注解,但并没有使用A ...

  2. Spring MVC 中使用AOP 进行事务管理--XML配置实现

    1.今天写一篇使用AOP进行事务管理的示例,关于事务首先需要了解以下几点 (1)事务的特性 原子性(Atomicity):事务是一个原子操作,由一系列动作组成.事务的原子性确保动作要么全部完成,要么完 ...

  3. Spring AOP基于注解的“零配置”方式实现

    为了在Spring中启动@AspectJ支持,需要在类加载路径下新增两个AspectJ库:aspectjweaver.jar和aspectjrt.jar.除此之外,Spring AOP还需要依赖一个a ...

  4. 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)

    [AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...

  5. 8 -- 深入使用Spring -- 4...5 AOP代理:基于注解的“零配置”方式

    8.4.5 基于注解的“零配置”方式 AspectJ允许使用注解定义切面.切入点和增强处理,而Spring框架则可识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5 一样的 ...

  6. 【学习】Spring 的 AOP :基于Annotation 的“零配置”方式

    转自:http://www.cnblogs.com/jbelial/archive/2012/07/20/2539123.html AOP(Aspect Orient Programming ) , ...

  7. spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

    1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...

  8. 基于AspectJ的XML方式进行AOP开发

    -------------------siwuxie095                                 基于 AspectJ 的 XML 方式进行 AOP 开发         1 ...

  9. Spring之AOP原理、代码、使用详解(XML配置方式)

    Spring 的两大核心,一是IOC,另一个是AOP,本博客从原理.AOP代码以及AOP使用三个方向来讲AOP.先给出一张AOP相关的结构图,可以放大查看. 一.Spring AOP 接口设计 1.P ...

随机推荐

  1. python 日记 day4

    1.为何数据要分类 数据是用来表示状态的,不同的状态应该用不同类型的数据来表示. 2.数据类型 数字 字符串 列表 元组 字典 集合 列表:列表相比于字符串,不仅可以储存不同的数据类型,而且可以储存大 ...

  2. mysql利用MySQLWorkbench生成数据表之间的关系图

    先看结果,默认是展开的,我手动把表折叠了 那么如何实现呢 先点击这里 然后通过向导来创建即可,一直到finish就行了

  3. WhaleCTF之web密码泄露

    WhaleCTF之密码泄露 前往题目 没有思路,习惯看一下源码,拉到最后,发现有惊喜 直接把index.php 换成password.txt,访问 这是要让我密码爆破吗?直接把密码保存成passwor ...

  4. jvm 分代回收算法通俗理解

    jvm区域总体分两类,heap区和非heap区.heap区又分:Eden Space(伊甸园).Survivor Space(幸存者区).Tenured Gen(老年代-养老区). 非heap区又分: ...

  5. Python - 集合与元素之集合定义和基本操作方法

    集合(set) 定义:由不同元素组成的集合,集合中是一组无序排列可hash的值(不可变的值)例如数字.字符串.元组,可以作为字典的key 定义集合: # 定义集合 s = {1, 2, 3, 3, 3 ...

  6. SpringBoot Jar应用Linux后台部署执行

    nohup java -jar shop-h5.jar > log_h5.file 2>&1 &

  7. Windows 常用工具 & 开发工具 & Chrome插件 & Firefox 插件 & 办公软件

    常用工具 1.FastStone 8.0 2.印象笔记 3.Chrome 4.Beyond Compare 5.Everything 6.有道词典 7.文本编辑软件 EditPlus UltraEdi ...

  8. Odoo 在 Ubuntu 环境下性能调优

    一.首先我们要分析影响odoo 服务器 性能的因素 CPU 目前大部分CPU在同一时间只能运行一个线程,超线程的处理器可以在同一时间处理多个线程,因此可以利用超线程特性提高系统性能. 在linux系统 ...

  9. js移动端判断上下左右划屏

    $(function(){ (function(){ var LSwiperMaker = function(o){ var that = this; this.config = o; this.co ...

  10. java并发系列(二)-----线程之间的协作(wait、notify、join、CountDownLatch、CyclicBarrier)

    在java中,线程之间的切换是由操作系统说了算的,操作系统会给每个线程分配一个时间片,在时间片到期之后,线程让出cpu资源,由其他线程一起抢夺,那么如果开发想自己去在一定程度上(因为没办法100%控制 ...