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. JAVA中精确计算金额BigDecimal

    package com.chauvet.utils; import java.math.BigDecimal; import java.text.DecimalFormat; import java. ...

  2. maven 几个插件的使用

    1.  compile <plugin> <artifactId>maven-compiler-plugin</artifactId> <configurat ...

  3. mac10.9+php5.5.15+brew0.9.5的安装

      Brew 是 Mac 下面的包管理工具,通过 Github 托管适合 Mac 的编译配置以及 Patch,可以方便的安装开发工具. Mac 自带ruby 所以安装起来很方便,同时它也会自动把git ...

  4. NetCFSvcUtil.exe and Windows 7

    NetCFSvcUtil.exe is the analogous of Svcutil.exe but for devices. You can use it to generate a WCF c ...

  5. Spring Boot构建RESTful API与单元测试

    如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建议先看一下相关的内容. @Controller:修饰class,用来创建处理http请求的对象 @RestController:Spr ...

  6. SQL Server2005作业执行失败的解决办法

    数据库:SQL Server 2005,运行环境:Windows Server 2008  在数据库里的所有作业都执行失败,包括自动执行和手动执行.在事件查看器里看到的错误报告如下: 该作业失败.  ...

  7. 黄聪:MySQL 按指定字段自定义列表排序

    问题描述 大家都知道, MySQL 中按某字段升序排列的 SQL 为 (以 id 为例, 下同): SELECT * FROM `MyTable` , , , ) ORDER BY `id` ASC ...

  8. web.xml 的加载过程

    初始化过程: 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>. 接着容器会创建一个Serv ...

  9. eclipse默认文件编码

    eclipse里 就是在eclipse.ini文件里添加一行-Dfile.encoding=utf-8即可 -startup plugins/org.eclipse.equinox.launcher_ ...

  10. git 新建项目

    备份. 1.cd dirctory 2.git init 3.git.oschina.net 上新建项目. 4.git remote add origin XXX.git 5.git push ...