Spring aop——前置增强和后置增强 使用注解Aspect和非侵入式配置

  

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

原文地址:https://www.cnblogs.com/john69-/p/5962795.html