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. 软件-MQ-MQ:IBM MQ

    ylbtech-软件-MQ-MQ:MQ(IBM MQ) MQ传递主干,在世界屡获殊荣. 它帮您搭建企业服务总线(ESB)的基础传输层.IBM WebSphere MQ为SOA提供可靠的消息传递.它为经 ...

  2. WPF DataGrid 数据绑定之"List配合Dictionary"

    WPF 的DataGrid是WPF中最为强大的控件之一,可以通过各种方式绑定 例如通过最为形似的dataTable来绑定 本文则用List<Dictionary<K,V>>来绑 ...

  3. MySQL时间格式转换

    1.时间转换成特定字符串 例:select DATE_FORMAT(now(),'%Y-%m-%d %H:%i::%s'); --> '2019-10-16 10:59::18' 2.一种字符串 ...

  4. 使用ssh时报错:Service对象空指针异常

    有可能是spring容器不能自动生成service对象,导致空指针异常,常见的情况可能是在service前面加@Service注释

  5. 免费提取百度文库 doc 文件

    首先说明,今天要推荐的这款软件,不能不能不能免费提取百度文库里 PDF 格式的文件. 对于其他的格式,无论收费与否都能免费提取. 只是口头说说免不了耍流氓的嫌疑,举栗如下: 百度文库里<喜迎党的 ...

  6. 2019-8-31-dotnet-通过-WMI-获取指定进程的输入命令行

    title author date CreateTime categories dotnet 通过 WMI 获取指定进程的输入命令行 lindexi 2019-08-31 16:55:59 +0800 ...

  7. MySQL系列(五)--二进制日志对主从复制的影响

    MySQL复制是基于主库上的二进制日志来完成,复制是异步的,可能存在延迟 MySQL日志分为: 1.服务层日志:二进制日志.通用日志.慢查日志 2.存储引擎层日志:innodb中重做日志和回滚日志 二 ...

  8. m邻接

    <冈萨雷斯 数字图像处理(第三版)> http://www.cnblogs.com/liugl7/p/5249453.html http://www.cnblogs.com/carfiel ...

  9. mysql连接出现Unknown system variable 'tx_isolation'异常

    出现这个异常,是因为mysql-connector-java.jar的版本太低,数据库的版本太高,不匹配导致的. 因此将mysql-connector-java升级到最新版本就解决了问题. 最新的三个 ...

  10. Hibernate_添加联系人练习

    分析: 联系人与客户是多对一,一个客户(公司)有多个联系人,在多的这一方,即LinkMan, 1.LinkMan.java中除自身属性外,还需要 2.在hbm.xml文件中,加上 意思是建立一个外键用 ...