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. C#.NET常见问题(FAQ)-如何在系统变量中加入新的环境变量

    比如我要将C:\Windows\Microsoft.NET\Framework\v3.5这个目录加入环境变量 则在系统的环境变量中点击Path,编辑,然后加入一个分号";",然后粘 ...

  2. HttpContext.Current.Cache 和 HttpRuntime.Cache

    HttpRuntime.Cache:用于winfrom 和 web HttpContext.Current.Cache 用于web .NET中Cache有两种调用方式:HttpContext.Curr ...

  3. eclipse开发php的插件

    php development tools,简称pdt,在Eclipse marketplace里面可以下载到.

  4. spring mvc返回jsonp内容

    代码如下: import com.alibaba.fastjson.JSONPObject; @RequestMapping(value = "/method1") @Respon ...

  5. Android 四大组件之 Activity(二)

    1.综述 Activity是Android四大组件(Application Components)之一,简单来说Activity就是平常所见到的用户界面,一般情况下,一个Activity所占的窗口是满 ...

  6. PyQt5——安装Eric6

    Eric6是PyQt编程最理想的IDE.windows版的安装很简单.下面的安装也是在windows上进行的.linux版的我安装有点问题,有时间再折腾. 下载: Eric6官网:http://eri ...

  7. HTML解析模块

    import html html.escape(s, quote=True) 对特殊字符进行转义 Convert the characters &, < and > in stri ...

  8. Deep compression code

    https://github.com/songhan/SqueezeNet-Deep-Compression import sys import os import numpy as np impor ...

  9. $nextTick 宏任务 微任务 macrotasks microtasks

    1.nextTick调用方法 首先看nextTick的调用方法: https://cn.vuejs.org/v2/api/#Vue-nextTick // 修改数据 vm.msg = 'Hello' ...

  10. VC线程同步方法

    VC MFC中线程同步对象的区别 临界区 CCriticalSection,在用户模式工作,适用于保护线程间共享资源,一个线程可以多次Lock不会出错.不支持在多进程之间工作.互斥量 CM ...