前面主要介绍了如何通过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();
		
		
				
						  }
		
		
				
						  
						
						
				
		
		
				}
		
		 
	posted on 2006-09-15 19:31 
布衣郎 阅读(1656) 
评论(1)  编辑  收藏  所属分类: 
aop 、
spring