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 ...
随机推荐
- PHP实现记录浏览历史页面
<?php /******* 说明:cookie只能保存字符串 本实例中,需要保存多个URL(历史访问记录),思路是先将URL数组转为字符串,然后保存,读取时,再循环读取 *******/ // ...
- 关于JEECMS套站工具的使用要点
第一步:在[界面—资源]下面引入资源文件(js,css,img…) 第二步:在[界面—模板]下面将网站的入口页面写在[index]文件下 此时修改index页面中的 js,css,图片 的路径,路 ...
- [jeecms]获取父栏目下的子栏目名称
[@cms_channel_list parentId='父栏目id'] [#list tag_list as c] <a href="${c.url}">${c.na ...
- 【agc019f】AtCoder Grand Contest 019 F - Yes or No
题意 有n个问题答案为YES,m个问题答案为NO. 你只知道剩下的问题的答案分布情况. 问回答完N+M个问题,最优策略下的期望正确数. 解法 首先确定最优策略, 对于\(n<m\)的情况,肯定回 ...
- HashMap基础知识
HashMap 简介 HashMap 主要用来存放键值对,它基于哈希表的Map接口实现,是常用的Java集合之一. JDK1.8 之前 HashMap 由 数组+链表 组成的,数组是 HashMap ...
- vim编辑器操作②
本文主要介绍vim的常用编辑命令: 字符编辑: x:删除光标所在处的字符: #x:删除光标所在处起始的#个字符: 替换命令: r:替换光标所在处的字符: rCHAR; 例如:替换list中的l为大写L ...
- c++新特性实验(5)声明与定义:属性列表(C++11 起)
1.初识属性 1.1 实验A: noreturn 属性 [[ noreturn ]] static void thread1(void *data){ cout << "nore ...
- TZ_08_maven把第三方 jar 包放入本地仓库或私服
--安装第三方jar包到本地仓库 需求:首先下载jar包并且找到对应的 -DgroupId=? -DartifactId=? -Dversion=? -Dpackaging=jar -Dfile=j ...
- iview 小问题笔记
总结一下用 iview 组件期间遇到的小问题.一边做项目一边做笔记. 官方文档:iview API 按需引入: 1,iview 分页组件 <Page :total="aboutPage ...
- draft.js开发富文本编辑器
写在前头的话 在react中去寻找一个好用的富文本编辑器网上很少有推荐的,搜到的也只有一些个人不成熟的作品,慢慢发现网上比较推荐的一个东东叫做draft.js. 这个东西在网上可以找到的教程也是手指头 ...