如果项目中不能使用JDK5.0,那就无法使用基于@AspectJ注解的切面。但是我们仍可以使用AspectJ切点表达式,可以用Schema配置的方法来代替。
首先定义一个增强:
注意该增强中没有注解也没有继承任何的接口,只是一个普通的类。该如何让spring辨别出它是一个增强呢?这就需要spring配置了,代码如下:
<aop:config>中的proxy-target-class可以决定使用哪种代理技术,设置为true表示使用cglib代理,false表示使用jdk动态代理,<aop:config>可以定义多个,不同的<aop:config>可以采用不同的代理技术。
<aop:aspect>中可以定义多个增强,上面的例子中<aop:before>定义的是前置增强,<aop:after-returning>定义的是后置增强。还可以用<aop:around>定义环绕增强,<aop:after-throwing>定义异常抛出异常,<aop:after>定义final增强(相当于后置增强和异常抛出增强的联合体),<aop:declare-parents>定义引介增强。
<aop:after-returning>中的method指定了增强类中的增强方法;pointcut用来指定切面,采用的是AspectJ表达式;returning指定的是返回值在增强方法中的参数名,与上一篇中@AspectJ注解增强相似。
使用schema还可以用旧方法定义的增强:
首先定义一个增强:
package com.maxjay.bank.advice.schema;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import com.maxjay.bank.model.TSysUser;
public class LoggerAdvice {
private Logger logger = Logger.getLogger(LoggerAdvice.class);
/**
* JoinPoint为连接点的信息,可以通过jp获取到代理类的类型、要执行的方法及其参数
* @param jp
*/
public void preLogger(JoinPoint jp) {
logger.info("前置增强,使用schema方法配置");
StringBuffer sb = new StringBuffer();
for(Object s:jp.getArgs()){
sb.append(s.toString()+",");
}
logger.info("代理对象执行的方法名为:" + jp.getSignature().getName() + ",方法参数为:"
+ sb.toString());
}
/**
* JoinPoint为连接点的信息,可以通过jp获取到代理类的类型、要执行的方法及其参数
* @param jp
* @param user 代理方法的返回值
*/
public void postLogger(JoinPoint jp, TSysUser user) {
logger.info("后置增强,代理对象执行的结果返回值为:" + user.getUserName());
}
}
注意该增强中没有注解也没有继承任何的接口,只是一个普通的类。该如何让spring辨别出它是一个增强呢?这就需要spring配置了,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
此段头部声明必须添加,尤其是aop的声明部分,否则下面的配置无法使用
.........
<!-- 使用schema方式配置aop -->
<!-- 将要使用的增强类定义成bean -->
<bean id="loggerAdvice" class="com.maxjay.bank.advice.schema.LoggerAdvice" />
<aop:config proxy-target-class="true">
<aop:aspect ref="loggerAdvice">
<aop:before method="preLogger"
pointcut="target(com.maxjay.bank.service.impl.TestAdviceService) and execution(* validateUser(..))" />
<aop:after-returning method="postLogger"
pointcut="target(com.maxjay.bank.service.impl.TestAdviceService) and execution(* validateUser(..))" returning="user"/>
</aop:aspect>
</aop:config>
<aop:config>中的proxy-target-class可以决定使用哪种代理技术,设置为true表示使用cglib代理,false表示使用jdk动态代理,<aop:config>可以定义多个,不同的<aop:config>可以采用不同的代理技术。
<aop:aspect>中可以定义多个增强,上面的例子中<aop:before>定义的是前置增强,<aop:after-returning>定义的是后置增强。还可以用<aop:around>定义环绕增强,<aop:after-throwing>定义异常抛出异常,<aop:after>定义final增强(相当于后置增强和异常抛出增强的联合体),<aop:declare-parents>定义引介增强。
<aop:after-returning>中的method指定了增强类中的增强方法;pointcut用来指定切面,采用的是AspectJ表达式;returning指定的是返回值在增强方法中的参数名,与上一篇中@AspectJ注解增强相似。
使用schema还可以用旧方法定义的增强:
<!-- 使用文章(一)中用接口方法定义的增强 -->
<bean id="loggerBeforeAdvice" class="com.maxjay.bank.advice.LoggerBeforeAdvice" />
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="loggerBeforeAdvice"
pointcut="target(com.maxjay.bank.service.impl.TestAdviceService) and execution(* validateUser(..))" />
</aop:config>