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. php 单引号 双引号 ,php字符串/ hmtl / 数据库显示/ 及php的几个转化函数

    * 以单引号为定界符的php字符串,支持两个转义\'和\\* 以双引号为定界符的php字符串,支持下列转义(\'会直接输出\' ,也会转义 \\):    \n 换行(LF 或 ASCII 字符 0x ...

  2. java之main

    Java中用户向系统传递参数的三种基本方式 main方法 在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的 ...

  3. mysql 主从 重新同步

    mysql 主从同步一担出了问题之后,就会导致从库上的数据和主库不一样了.所以需要生新同步数据. 1.登录主库服务器,进入mysql,命令为:mysql -uroot -ppassword 2.执行: ...

  4. c++ poco库https调用

    #include "Poco\File.h"#include "Poco\FileStream.h"#include "Poco\Process.h& ...

  5. 读文档readarx.chm

    readarx.chm <Tips and Techniques> Incremented AutoCAD Registry Number Ideally, a change of reg ...

  6. ssh远程连接错误

    在平时工作中,有时候需要SSH登陆到别的Linux主机上去,但有时候SSH登陆会被禁止,并弹出如下类似提示: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  7. BYTE 和字符串转换

    string hanzi = "汉字"; byte[] arr = System.Text.Encoding.UTF8.GetBytes(hanzi); string temp = ...

  8. chrome常用配置

    插件类: Gestures for Google Chrome 增加鼠标手势 安装后需重启 JSONView 自动格式化页面输出的json数据

  9. 创建MySQL 用户

    create user 'lixianming'@'localhost' identified by 'lxm123456'; grant all on art.* to 'lixianming'@' ...

  10. ASP.NET MVC IOC 之AutoFac攻略

    一.为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用AutoFac的貌似更为普遍,于是捯饬了两天,发现这个东东确实是个高大上的IOC容器~ Autofa ...