Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7583523 建立时间:2006年5月29日 |

| |
[Spring]spring 2.0 aop编程 软件技术
lhwork 发表于 2006/10/10 10:05:08 |
在spring2.0中,aop发生了很大的变化:主要分为两大方面1.支持简单的aop xml配置2.支持@AspectJ的注释先来看一看第一种情况:申明一个aspect,在xml中的申明如下:<aop:config> <aop:aspect id="myAspect" ref="aBean"> ... </aop:aspect></aop:config><bean id="aBean" class="..."> ...</bean>申明pointcut<aop:config> <aop:pointcut id="businessService" expression="execution(* com.xyz.myapp.service.*.*(..))"/></aop:config>申明adviceBefore advice:<aop:aspect id="beforeExample" ref="aBean"> <aop:before pointcut-ref="dataAccessOperation" method="doAccessCheck"/></aop:aspect>After returning advice:<aop:aspect id="afterReturningExample" ref="aBean"> <aop:after-returning pointcut-ref="dataAccessOperation" method="doAccessCheck"/> ... </aop:aspect>或者带有返回参数<aop:aspect id="afterReturningExample" ref="aBean"> <aop:after-returning pointcut-ref="dataAccessOperation" returning="retVal" method="doAccessCheck"/> ... </aop:aspect> After throwing advice:<aop:aspect id="afterThrowingExample" ref="aBean"> <aop:after-throwing pointcut-ref="dataAccessOperation" method="doRecoveryActions"/> ... </aop:aspect>或者带有throwing<aop:aspect id="afterThrowingExample" ref="aBean"> <aop:after-throwing pointcut-ref="dataAccessOperation" throwing="dataAccessEx" method="doRecoveryActions"/> ... </aop:aspect>After (finally) advice:<aop:aspect id="afterFinallyExample" ref="aBean"> <aop:after pointcut-ref="dataAccessOperation" method="doReleaseLock"/> ... </aop:aspect>Around advice:<aop:aspect id="aroundExample" ref="aBean"> <aop:around pointcut-ref="businessService" method="doBasicProfiling"/> ... </aop:aspect>Advice parameters:<aop:before pointcut="Pointcuts.anyPublicMethod() and @annotation(auditable)" method="audit" arg-names="auditable"/>对于引入接口(Introductions):<aop:aspect id="usageTrackerAspect" ref="usageTracking"> <aop:declare-parents types-matching="com.xzy.myapp.service.*+", implement-interface="UsageTracked" default-impl=" service.tracking.DefaultUsageTracked"/> <aop:before pointcut="com.xyz.myapp.SystemArchitecture.businessService() and this(usageTracked)" method="recordUsage"/></aop:aspect>前面主要介绍了如何通过xml实现aop编程,下面主要介绍如何通过@AspectJ来实现。为了使@AspectJ 支持生效,需要做以下步骤:在xml中设置
<aop:aspectj-autoproxy/>
或者在xml中加入<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />声明 aspect
<bean id="myAspect" class="org.xyz.NotVeryUsefulAspect">
<!-- configure properties of aspect here as normal -->
</bean>
package org.xyz;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class NotVeryUsefulAspect {
}
声明
pointcut
@Pointcut("execution(* transfer(..))")
public void transfer() {}
声明
advice
Before advice:
@Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
public void doAccessCheck() {
// ...
}
After returning advice:
@AfterReturning("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
public void doAccessCheck() {
// ...
}
或者
@AfterReturning(pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",returning="retVal")
public void doAccessCheck(Object retVal) {
// ...
}
After throwing advice:
@AfterThrowing("SystemArchitecture.dataAccessOperation()")
public void doRecoveryActions() {
// ...
}
或者
@AfterThrowing(
pointcut=" SystemArchitecture.dataAccessOperation()",
throwing="ex")
public void doRecoveryActions(DataAccessException ex) {
// ...
}
After (finally) advice:
@After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
public void doReleaseLock() {
// ...
}
Around advice:
@Around("com.xyz.myapp.SystemArchitecture.businessService()")
public Object doBasicProfiling(
ProceedingJoinPoint
pjp) throws Throwable {
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
return retVal;
}
Advice parameters:
@Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation() &&" +
"args(account,..)"
)
public void validateAccount(Account account) {
// ...
}
声明参数名称:
@Before(
value="com.xyz.lib.Pointcuts.anyPublicMethod() && " +
"@annotation(auditable)",
argNames="auditable"
)
public void audit(Auditable auditable) {
AuditCode code = auditable.value();
// ...
}
Advice 排序:
一般以声明的方法次序为先后
不同的
Advice
,通过实现
Ordered
接口,来排序
Introductions
用于引入新的接口
@Aspect
public class UsageTracking {
@DeclareParents(value="com.xzy.myapp.service.*+",
defaultImpl=DefaultUsageTracked.class)
public static UsageTracked mixin;
@Before("com.xyz.myapp.SystemArchitecture.businessService() &&" +
"this(usageTracked)")
public void recordUsage(UsageTracked usageTracked) {
usageTracked.incrementUseCount();
}
}前面这是讲了许多的概念,下面以一个例子来说明:一个很好理解的aop 例子,也就是日志服务。先从aop第一种方式来实现,也就是xml配置方式先创建基本的日志类:public class Logger {
private static Log log = LogFactory.getLog(Logger.class);
public void entry(String message) { log.info(message); }}这里只是简单的一个方法,当然实际情况可能不同。由于xml配置需要一个方面的实现bean所以创建一个简单的bean :public class LogBean {
private Logger logger = new Logger();
public Object aroundLogCalls(ProceedingJoinPoint joinPoint) throws Throwable { logger.entry("before invoke method:" + joinPoint.getSignature().getName()); Object object = joinPoint.proceed(); logger.entry("after invoke method:" + joinPoint.getSignature().getName()); return object; }}这里采取简单的around advice,其他类型的advice 基本上都差不多当然有了这两个核心的日志类,需要一个测试类,用于测试。public class TestBean {
public void method1() { System.out.println("in method1"); }
public void method2() { System.out.println("in method2"); }}这就是需要测试的类了,需要记录日志的方法只有两个,这里用System.out.println,只是想显示方法的调用顺序。然后关键的在于xml的配置了<?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" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:config> <!-- expression 表示要执行的匹配表达式,这里匹配所有的public方法,但是去除logger类的所有方法,防止无限调用--> <aop:pointcut id="loggableCalls" expression="execution(public * *(..)) and !execution(* org.spring.test.aop.log.Logger.*(..))"/> <aop:aspect id="logAspect" ref="logBean"> <aop:around pointcut-ref="loggableCalls" method="aroundLogCalls"/> </aop:aspect> </aop:config> <bean id="logBean" class="org.spring.test.aop.log.LogBean" /> <bean id="testBean" class="org.spring.test.aop.log.TestBean"/> </beans>现在写一个测试类:public class LogXmlTest extends RootTest {
@Override protected String getBeanXml() { return "org/spring/test/aop/log/bean.xml"; }
public void testLog() { TestBean bean = (TestBean) ctx.getBean("testBean"); bean.method1(); bean.method2(); }
}public abstract class RootTest extends TestCase {
protected ApplicationContext ctx;
protected Log log = LogFactory.getLog(getClass());
protected RootTest() { ctx = new ClassPathXmlApplicationContext(getBeanXml()); }
protected abstract String getBeanXml();
}打印的消息如下:2006-09-17 11:08:28,203 INFO [org.spring.test.aop.log.Logger] - before invoke method:method1in method12006-09-17 11:08:28,203 INFO [org.spring.test.aop.log.Logger] - after invoke method:method12006-09-17 11:08:28,218 INFO [org.spring.test.aop.log.Logger] - before invoke method:method2in method22006-09-17 11:08:28,218 INFO [org.spring.test.aop.log.Logger] - after invoke method:method2第二种实现方式,采用注释方式:Logger 类不变创建一个LogAspect类@Aspectpublic class LogAspect {
private Logger logger = new Logger();
@Pointcut("execution(public * *(..))") public void publicMethods() {
}
@Pointcut("execution(* org.spring.test.aop.log.Logger.*(..))") public void logObjectCalls() {
}
@Pointcut("publicMethods()&&!logObjectCalls()") public void loggableCalls() {
}
@Around("loggableCalls()") public Object aroundLogCalls(ProceedingJoinPoint joinPoint) throws Throwable { logger.entry("before invoke method:" + joinPoint.getSignature().getName()); Object object = joinPoint.proceed(); logger.entry("after invoke method:" + joinPoint.getSignature().getName()); return object; }}配置文件就简单多了<?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" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy/> <!-- 或者使用以下定义 <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> --> <bean id="logAspect" class="org.spring.test.aop.log.LogAspect"/> <bean id="testBean" class="org.spring.test.aop.log.TestBean"/> </beans>测试类:跟上面的差不多把xml文件换掉就行打印的方式差不多个人还是比较喜欢第二种实现。 |
|
|