AspectJ是一个面向切面的框架,它扩展了java语言,定义了AOP语法,
能够在编译期提供代码的织入,所以它有一个专门的编译器用来生成遵守字节码字节编码规范的Class文件

确保使用jdk为5.0以上版本。

01.使用注解标注增强(AspectJ)  :取代了配置文件中的aop:pointcut节点的配置

添加jar和log4j的配置文件

aspectj-1.8.7.jar

aspectjweaver.jar

添加头文件:

xmlns:aop="http://www.springframework.org/schema/aop"
xsi:添加:(可以在文档里找:"40.2.6 the tx (transaction) schema")
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd

在beans子节点只需要添加<aop:aspectj-autoproxy/>元素,就可以启用对于@AspectJ注解的支持,
Spring将自动为匹配的Bean创建代理;
控制反转:还要声明UserLogger的一个实例:<bean class="aspectj_aop.UserLogger"></bean>

★使用@Aspect标识将一个类交给Spring容器管理
@Aspect
public class UserLogger {
private static final Logger log = Logger.getLogger(UserLogger.class);

//匹配 所有类型返回值的,proxy_dynamic包中的,所有方法名,所有参数个数和类型
@Before("execution(* aspectj_dao.Dosomthing.*())")
  public void before(){
  System.out.println("前置增强执行111!");
}

@AfterReturning("execution(* aspectj_dao.Dosomthing.*())")
  public void after(){
  System.out.println("前置增强执行222!");
  }
}

★ 在上面的操作中 @Before & @AfterReturning需要分别设置切点:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?
name-pattern(param-pattern) throws-pattern?)
除了返回类型模式(上面代码片段中的ret-type-pattern),名字模式和参数模式以外,
所有的部分都是可选的,返回类型模式觉得了方法的返回类型必须依次匹配一个连接点。
使用最频繁的返回类型模式是*,他代表了匹配任意的返回类型。一个全限定的类型名称将只会匹配返回给定类型的方法。
名字模式匹配的是方法名。可以使用*作为所有或者部分命名模式。参数模式稍微有点复杂:()匹配了一个不接受任何参数的方法,
而(..)匹配了一个解释任意类型参数的方法(零个或者更多个)。
模式(*)匹配了一个接受任何类型的参数的方法。模式(*,String)匹配了一个接受两个参数的方法,
第一个可以是任意类型,第二个必须是String类型。

02.使用JoinPoint操作
@Before("execution(* aspectj_dao.Dosomthing.*())")
public void before(JoinPoint point){
System.out.println("前置增强执行111!");
System.out.println("target对象:"+point.getTarget());
System.out.println("方法:"+point.getSignature().getName());
System.out.println("参数个数:"+point.getArgs().length);

}

@AfterReturning(value="execution(* aspectj_dao.Dosomthing.*())",returning="returnvalue")
public void afterReturning(JoinPoint point,Object returnvalue){
System.out.println("前置增强执行222!");
System.out.println(point.getSignature().getName()+"方法的返回值为:"+returnvalue);
}

/**
* java.lang.Object[] getArgs():获取连接点方法运行时的入参列表
* Signature getSignature():获取连接点的方法前面对象
* java.lang.Object getTarget():获取连接点所在的目标对象
* java.lang.Object getThis():获取代理对象本身
*/

//环绕增强
@Around(value="execution(* aspectj_dao.Dosomthing.*())")
  public void around(ProceedingJoinPoint pjp) throws Throwable{
  System.out.println("环绕前置增强");
  pjp.proceed();
  System.out.println("环绕后置增强");
}

//异常增强
@AfterThrowing(value="execution(* aspectj_dao.Dosomthing.*())")
  public void afterThrowing() throws Throwable{
  System.out.println("异常增强");
}

//最终增强(无论如何都要执行的此方法)
@After(value="execution(* aspectj_dao.Dosomthing.*())")
  public void after() throws Throwable{
  System.out.println("最终增强、、、");
}

———————————————分割线———————————————————————————————————————————

03.Aspect 基于xml的注解配置(使用POJO作为增强类)实现Spring与程序的解耦
在此更换了applicationContext.xml文档为 applicationContextxml.xml

☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
//01.前置增强 匹配 所有类型返回值的,proxy_dynamic包中的,所有方法名,所有参数个数和类型
public void before(JoinPoint point){
  System.out.println("前置增强执行111!");
}

//02.后置增强
public void afterReturning(JoinPoint point,Object returnvalue){
  System.out.println("后置增强执行222!");
  //System.out.println("getTarget对象:"+point.getTarget().getClass());
  //System.out.println("getThis对象:"+point.getThis().getClass());
  //System.out.println(point.getSignature().getName()+"方法的返回值为:"+returnvalue);
}

//03.环绕增强
public void around(ProceedingJoinPoint pjp) throws Throwable{
  System.out.println("环绕前置增强");
  pjp.proceed();
  System.out.println("环绕后置增强");
}

//04.异常增强
public void afterThrowing() throws Throwable{
  System.out.println("异常增强");
}

//05.最终增强(无论如何都要执行的此方法)
public void after() throws Throwable{
  System.out.println("最终增强、、、");
}
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
以上是POJO的常见写法
如何让POJO实现增强?
在配置文件里面添加一下配置即可:
<!-- 声明增强方法所在的bean -->
<bean id="myAspect" class="aspectj_aop.UserLogger_xml"></bean>

<bean id="do1" class="aspectj_dao.Dosomthing"></bean>
<aop:config>
<aop:pointcut expression="execution(* aspectj_dao.Dosomthing.*())" id="mypoint"/>
<aop:aspect ref="myAspect">
<aop:before method="before" pointcut-ref="mypoint"/>
<aop:after-returning method="afterReturning" pointcut-ref="mypoint" returning="returnvalue"/>
<aop:after method="after" pointcut-ref="mypoint"/>
<aop:around method="around" pointcut-ref="mypoint"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="mypoint" />

</aop:aspect>
</aop:config>

实例链接:链接:http://pan.baidu.com/s/1miM00Uc 密码:3lfl

Spring aop——前置增强和后置增强 使用注解Aspect和非侵入式配置的更多相关文章

  1. Spring AOP前置通知和后置通知

    Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...

  2. Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等

    实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...

  3. Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。

    实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...

  4. Spring初学之annotation实现AOP前置通知和后置通知

    实现两个整数的加减乘除,并在每个计算前后打印出日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...

  5. 实现简单的AOP前置后置增强

    AOP操作是我们日常开发经常使用到的操作,例如都会用到的spring事务管理.今天我们通过一个demo实现对一个类的某一个方法进行前置和后置的增强. //被增强类 public class PetSt ...

  6. Thinkphp入门 二 —空操作、空模块、模块分组、前置操作、后置操作、跨模块调用(46)

    原文:Thinkphp入门 二 -空操作.空模块.模块分组.前置操作.后置操作.跨模块调用(46) [空操作处理] 看下列图: 实际情况:我们的User控制器没有hello()这个方法 一个对象去访问 ...

  7. thinkPHP 空模块和空操作、前置操作和后置操作 详细介绍(十四)

    原文:thinkPHP 空模块和空操作.前置操作和后置操作 详细介绍(十四) 本章节:介绍 TP 空模块和空操作.前置操作和后置操作 详细介绍 一.空模块和空操作 1.空操作 function _em ...

  8. eas之dep的前置脚本和后置脚本

    dep的前置脚本和后置脚本,什么时候写,是这样解释的:    前置脚本是在方法前执行,后置脚本是在方法后执行    1.比如保存扩展,如果你要在保存前校验某个字段的值,你要在前置脚本中写,如果要保存后 ...

  9. thinkPHP 空模块和空操作、前置操作和后置操作 具体介绍(十四)

    本章节:介绍 TP 空模块和空操作.前置操作和后置操作 具体介绍 一.空模块和空操作 1.空操作 function _empty($name){ $this->show("$name ...

随机推荐

  1. JDK源码解读之toUnsignedString

    我们知道,所有整数都是通过二进制编码的形式存储在内存中的.比如32位的整数,最高位是符号位,0代表正数,1代表负数. 那么怎么才能够将整数的二进制编码形式打印出来呢?Integer类提供了一个公有静态 ...

  2. C++访问托管类(C#类库)

    1.新建C# 类库项目Airth,新建类 using System; using System.Collections.Generic; using System.Linq; using System ...

  3. Python Queue队列

    queue is especially useful in threaded programming when information must be exchanged safely between ...

  4. SQL2005 遍历表插入

    /* sql2005遍历表(方法1) insert into 数据表(userid,adddate) values((select userid from 用户表),date); */ /*sql20 ...

  5. SilverlightERP&CRM源码(可用于开发基于Silverlight的CRM,OA,HR,进销存等)

    SilverlightERP系统源代码(支持创建OA.SilverlightCRM.HR.进销存.财务等系统之用) 可用于开发以下系统 SilverlightERP SilverlightCRM Si ...

  6. Mysql 自定义HASH索引带来的巨大性能提升----[挖坑篇]

    有这样一个业务场景,需要在2个表里比较存在于A表,不存在于B表的数据.表结构如下: T_SETTINGS_BACKUP | CREATE TABLE `T_SETTINGS_BACKUP` ( `FI ...

  7. HTML 事件属性_03

    全局事件属性 HTML 4 的新特性之一是可以使 HTML 事件触发浏览器中的行为,比方说当用户点击某个 HTML 元素时启动一段 JavaScript. 如果你想学习更多关于事件属性,请访问 Jav ...

  8. Android学习笔记(三)

    在手机应用中菜单是很重要的一部分,它能够以直观的界面让用户去选择,现在我们学习如何去新建一个菜单. 首先在res目录下新建一个menu文件夹,然后在menu文件夹下新建一个Android XML Fi ...

  9. [Tex学习]编号

    \documentclass{ctexart}\usepackage{enumerate}\begin{document}\begin{enumerate}[{case}1]\item new\ite ...

  10. SQL%NOTFOUND

    异常相关: 源地址:https://zhidao.baidu.com/question/215737667.html EXCEPTIONwhen others thenrollback;dbms_ou ...