探讨AOP的两种实现方式

基于 AOP 的 XML架构

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">//定义一个切面
       <aop:pointcut id="businessService" //定义了一个切入点
        expression="execution(* com.tutorialspoint.Student.getName(..))"/>
        //切入点将与指定类的指定方法相匹配
       <aop:before pointcut-ref="businessService" //定义通知、关联切入点
         method="doRequiredTask"/>
       ...
   </aop:aspect>
</aop:config>

<bean id="aBean" class="..."> //将被配置和依赖注入
...
</bean> 

 

基于 AOP 的 @AspectJ

<aop:aspectj-autoproxy/> //@AspectJ 支持是可用的。
@Aspect
public class AspectModule {
}

@Pointcut("execution(* com.xyz.myapp.service.*.*(..))") 
private void businessService() {}  // id=方法名

@Before("businessService()")
public void doBeforeTask(){
 ...
}

@Before("execution(* com.xyz.myapp.service.*.*(..))")//直接内联切入点
public doBeforeTask(){
 ...
}

 

方法参数JoinPoint对象,可以获取目标方法信息。

dependency:aspectjweaver

posted @ 2020-07-09 16:39  赫拉克利特  阅读(215)  评论(0编辑  收藏  举报