怎么用spring Aop最简单最容易理解

1.先看一个例子java

 <aop:config>
        <aop:pointcut id="deleteOpus"
                      expression="execution(* net.qdedu.activity.service.OpusBizService.delOpus(..))"/>
        <aop:aspect ref="activityParticipationAop">
            <aop:after-returning method="deleteParticipationInfo" pointcut-ref="deleteOpus"
                                 returning="result"/>
        </aop:aspect>
    </aop:config>

 aop:pointcut 指的是切入点 切入点的id是 deleteOpusweb

 expression 指的是扫描对应的service类中的方法(delOpus),*表明的任意类型spring

 aop:aspect ref 指的是对应方法类(activityParticipationAop)express

 aop:after-returning method 指的是你本身在(activityParticipationAop)里面建立的对应方法(deleteParticipationInfo)spa

 ref 指的是 和切入点id 保持一致  id对应的是切点,ref就至关因而切面,他两个一相关联就能够执行那个service下面的方法了代理

 

2.对应的类code

@Component
public class ActivityParticipationAop {

  
}

 3.对应的方法xml

public void deleteParticipationInfo(JoinPoint jp){
  //获取做品的id
  IdParam idParam =(IdParam)jp.getArgs()[0];
}

 JoinPoint是和aop一块儿使用的类,它里面有4个参数 对象

Signature getSignature(); 	获取封装了署名信息的对象,在该对象中能够获取到目标方法名,所属类的Class等信息
Object[] getArgs(); 	获取传入目标方法的参数对象
Object getTarget(); 	获取被代理的对象
Object getThis(); 	获取代理对象

 4.增长切入点beanblog

 <bean id="paramValidate" class="com.we.core.web.aop.ParamValidate"></bean>
    <aop:config>
        <aop:pointcut id="businessMethod" expression="execution(* *..*.service..*.*(..))"/>
        <aop:aspect ref="paramValidate">
            <aop:before method="process" pointcut-ref="businessMethod"/>
        </aop:aspect>
    </aop:config>

 5.springcontext.xml

<!-- 打开aop 注解 -->
<aop:aspectj-autoproxy />
<!-- 扫描aop.xml -->
<import resource="aop.xml" />
相关文章
相关标签/搜索