AOP在spring中是非常重要的一个

在切面类中,有5种通知类型:

aop:before  前置通知

aop:after-returning  后置通知

aop:after  最终通知

aop:after-throwing  异常通知

aop:around  环绕通知

    <bean id="personDAO" class="com.lee.spring002.aop.xml.PersonDAOImpl"></bean>
<bean id="transaction" class="com.lee.spring002.aop.xml.Transaction"></bean> <aop:config >
<!-- 切入点表达式,作用:确定目标类 -->
<!-- spring 会自动检测这个表达式下的类是否是切面,如果是,则不会包含进来 -->
<aop:pointcut expression="execution(* com.lee.spring002.aop.xml.PersonDAOImpl.*(..))" id="perform"/>
<!-- ref 指向切面 -->
<aop:aspect ref="transaction">
<!-- 前置通知 -->
<aop:before method="beginTransaction" pointcut-ref="perform"/> <!--
后置通知
可以获取目标方法的返回值(前置方法获取不到)
如果目标方法抛出异常,后置通知则不会继续执行
-->
<aop:after-returning method="commit" pointcut-ref="perform" returning="val"/> <!--
最终通知
无论目标方法是否抛出异常都将执行此方法
-->
<aop:after method="finallyDisplay" pointcut-ref="perform"/> <!--
异常通知
-->
<aop:after-throwing method="exception" pointcut-ref="perform" throwing="content"/> <!--
环绕通知
能控制目标方法能否执行
前置通知和后置通知能在目标方法的前后加代码,但是不能控制方法的执行
-->
<aop:around method="arround" pointcut-ref="perform"/>
</aop:aspect>
</aop:config>

关于切面的表达式简单说一下:

  • 任意公共方法的执行:
    execution(public * *(..))
  • 任何一个名字以“set”开始的方法的执行:
    execution(* set*(..))
  • AccountService接口定义的任意方法的执行:
    execution(* com.xyz.service.AccountService.*(..))
  • 在service包中定义的任意方法的执行:
    execution(* com.xyz.service.*.*(..))
  • 在service包或其子包中定义的任意方法的执行:
    execution(* com.xyz.service..*.*(..))

IPersonDAO.java

 package com.lee.spring002.aop.xml;

 public interface IPersonDAO {
public String savePerson();
}

PersonDAOImpl.java

 package com.lee.spring002.aop.xml;

 public class PersonDAOImpl implements IPersonDAO {

     @Override
public String savePerson() {
System.out.println("PersonDAOImpl - savePerson()"); // int a = 1 / 0; return "save successfully";
} }

Transaction.java

 package com.lee.spring002.aop.xml;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 这是个切面
*
* @author leechenxiang
* @date 2016年1月12日
*
*/
public class Transaction { public void beginTransaction(JoinPoint jp) {
System.out.println("连接点名称: " + jp.getSignature().getName());
System.out.println("目标类名称: " + jp.getTarget().getClass());
System.out.println("Begin transaction...");
} /**
*
* @param jp
* @param val 目标方法返回值,要与returning对应
*/
public void commit(JoinPoint jp, Object val) {
System.out.println("Transaction commit..."); System.out.println("returning: " + val.toString());
} public void finallyDisplay() {
System.out.println("finally...");
} public void exception(JoinPoint jp, Throwable content) {
System.out.println("exception: " + content);
System.out.println("exception: " + content.getMessage());
} public void arround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("arround...");
pjp.proceed(); // 调用目标方法,如果这行不写,则目标方法不执行
}
}

测试:

 package com.lee.spring002.aop.xml;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TransactionTest { /**
* 原理:
* 1、当spring容器启动的时候,加载两个bean,对两个bean进行实例化
* 2、当spring容器对配置文件解析到<aop:config>的时候
* 3、把切入点表达式解析出来,按照切入点表达式匹配spring容器内容的bean
* 4、如果匹配成功,则为该bean创建代理对象
* 5、当客户端利用context.getBean获取一个对象时,如果该对象有代理对象,则返回代理对象
* 如果没有代理对象,则返回对象本身
*/
@Test
public void testPerson() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IPersonDAO personDAO = (IPersonDAO)context.getBean("personDAO");
personDAO.savePerson();
} }

github地址:https://github.com/leechenxiang/maven-spring002-aop

Spring 一二事(9) - xml 形式的 AOP的更多相关文章

  1. Spring 一二事(10) - annotation AOP

    先贴出POM的内容,这个毕竟是用的maven来简单构建的 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...

  2. Spring 一二事(8) - annotation 形式的 MVC

    <!-- component:把一个类放入到spring容器中,该类就是一个component 在base-package指定的包及子包下扫描所有的类 --> <context:co ...

  3. [原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Spring 一二事(7) - annotation

    之前的文章大多都是一带而过,一方面比较简单,一方面不是用的注解形式 在企业开发中,主要还是使用的注解来进行开发的 1 <!-- component:把一个类放入到spring容器中,该类就是一个 ...

  5. Spring 一二事(4) - 单例

    spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session 在配置spring mvc的action时 ...

  6. Spring 一二事(1)

    简单介绍一下spring,一方面带新手入入门,一方面自己也重温一下第一个小工厂先暂时不用maven,下一个会用maven来来配置 jar包只需要一个,spring版本为2.5(暂时为2.5,后续更新, ...

  7. Spring 一二事(5) - 依赖注入

    <!-- 依赖注入的装配过程 --> <bean id="person" class="com.lee.spring007.di.xml.setter. ...

  8. Spring 一二事(2)

    静态工厂方法及实例工厂的使用: applicationContext.xml: <!-- factory-method 是指调用静态工厂方法 --> <bean id="h ...

  9. Spring 一二事(6) - IOC MVC 简易搭建

    <bean id="personAction" class="com.lee.spring008.IOC.DI.MVC.PersonAction"> ...

随机推荐

  1. 如何使用FLASHGOT下载网页FLASH

    1 注意火狐的广告屏蔽插件可能将一些有用的东西屏蔽掉,从而无法得到广告FLASH, 2 随后即可在桌面上找到所需文件 你也可以按住A/T并单击FLASH文件(不论鼠标是否被替换为其他图形)迅雷会自动探 ...

  2. STL - 容器 - vector简单应用

    VectorTest.cpp #include <vector> #include <iostream> #include <string> #include &l ...

  3. wepy - 与原生有什么不同(request)

    关于request导入,清先查看这篇文档了解大概 缺陷: wx.request一个页面最多支持10个wx.request,况且不能保证请求先后顺序  对于wepy来说,使用了ES6 的Promise以 ...

  4. Web 前端攻防(2014版)-baidu ux前端研发部

    http://fex.baidu.com/articles/page2/ Web 前端攻防(2014版) zjcqoo | 20 Jun 2014 禁止一切外链资源 外链会产生站外请求,因此可以被利用 ...

  5. 算法笔记_182:历届试题 核桃的数量(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑).他的要求是: 1. ...

  6. Fiddler SessionFlags

    Each Session object in Fiddler contains a collection of string flags, in the Session.oFlags[] collec ...

  7. 笔试题之ejb

    ejb部分 1.EJB是基于哪些技术实现的?并说出SessionBean和EntityBean的区别,StatefulBean和StatelessBean的区别. EJB包括Session Bean. ...

  8. 用javascript的isNan()函数,可以判断是否为数字

    var getstockid = $.trim($("#SearchString").val()); if (!isNaN(getstockid)) alert('是数字!'); ...

  9. ASP.NET MVC:通过FileResult向浏览器发送文件

    在 Controller 中我们可以使用 FileResult 向客户端发送文件. FileResult FileResult 是一个抽象类,继承自 ActionResult.在 System.Web ...

  10. 机器学习的敲门砖:手把手教你TensorFlow初级入门

    摘要: 在开始使用机器学习算法之前,我们应该首先熟悉如何使用它们. 而本文就是通过对TensorFlow的一些基本特点的介绍,让你了解它是机器学习类库中的一个不错的选择. 本文由北邮@爱可可-爱生活  ...