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. Python 爬虫学习 网页图片下载

    使用正则表达式匹配 # coding:utf-8 import re import urllib def get_content(url): """ Evilxr, &q ...

  2. JavaScript 图片的上传前预览(兼容所有浏览器)

    功能描述 通过 JavaScript 实现图片的本地预览(无需上传至服务器),兼容所有浏览器(IE6&IE6+.Chrome.Firefox). 实现要点   ● 对于 Chrome.Fire ...

  3. 伪类after的使用

    .center-tc { text-align: center; position: relative; color:red; &:before{ content:''; width: 10p ...

  4. 转-利用Oracle审计功能来监测试环境的变化

    http://blog.csdn.net/luowangjun/article/details/5627102利用Oracle审计功能来监测试环境的变化 做过测试的人都应该会碰到这样的情况:测试发现的 ...

  5. Activity之间数据交流(onActivityResult的用法)

    http://www.blogjava.net/mixer-a/archive/2012/03/23/375014.html http://www.cnblogs.com/linjiqin/archi ...

  6. 【C】 05 - 声明和定义

    仅从形式上看,C程序就是由各种声明和定义组成的.它们是程序的骨架和外表,不仅定义了数据(变量),还定义了行为(函数).规范中的纯语言部分,声明和定义亦花去了最多的篇幅.完全说清定义的语法比较困难,这里 ...

  7. Java注释Override、Deprecated、SuppressWarnings详解(过时方法,即将删除的方法或成员变量)

    Override 这个注释的作用是标识某一个方法是否覆盖了它的父类的方法.那么为什么要标识呢?让我们来看看如果不用Override标识会发生什么事情. Deprecated 这个注释是一个标记注释.所 ...

  8. Type Project has no default.properties file! Edit the project properties to set one.

    Description Resource Path Location Type Project has no default.properties file! Edit the project pro ...

  9. [Eclipse] - 集成JBoss7热加载和自动发布

    使用Eclipse + JBoss开发时,总是要重启项目或JBoss,烦人.下面方法可以很简单的实现Eclipse + JBoss热加载和自动发布. 我的环境是JBoss 7.1.1 Final 1) ...

  10. php发送post包

    class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_in ...