xml配置
/aop:aspect
类:
public class test {
public void exe(){
System.out.println(new Date()+"进行了操作");
}
public void loginVerify(HttpServletRequest req1){
System.out.println("haha");
}
}
刚学习 spring aop 今天遇到一个问题,搞了一上午也没解决。 loginVerify方法里加一个参数就不能执行,如果去掉参数在访问页面就可以运行。
请问下这是什么原因,在线等~~
<aop:config >
<aop:aspect id="aoptest" ref="test">
<aop:before method="loginVerify" arg-names="req1" pointcut="within(web.netctoss.controller.FeeListController) and args(req1)"/>
<aop:after method="exe" pointcut="within(web.netctoss.controller.FeeListController)"/>
</aop:aspect>
</aop:config>
这个是xml配置
配置切面类
配置aop
aop:config
<!-- 配置切面类 -->
/aop:aspect
/aop:config
切入点表达式
* execution([修饰符] 返回值类型 包名.类名.方法名(参数))
例:execution(* com.*..*.*DaoImpl.save*(..))
AOP编写步骤
1.编写切面类
2.编写applicationContext.xml配置文件
配置切面类
<bean id="myAspectXml" class="切面类全限定名"/>
配置aop
<aop:config>
<!-- 配置切面类 -->
<aop:aspect ref="myAspectXml">
<aop:before method="writeLog" pointcut="切入点表达式"/>
</aop:aspect>
</aop:config>
3.切入点表达式
* execution([修饰符] 返回值类型 包名.类名.方法名(参数))
例:execution(* com.*..*.*DaoImpl.save*(..))
4.通知类型
1. 前置通知
* 在目标类的方法执行之前执行。
* 配置文件信息:<aop:after method="before" pointcut-ref="myPointcut3"/>
* 应用:可以对方法的参数来做校验
2. 最终通知
* 在目标类的方法执行之后执行,如果程序出现了异常,最终通知也会执行。
* 在配置文件中编写具体的配置:<aop:after method="after" pointcut-ref="myPointcut3"/>
* 应用:例如像释放资源
3. 后置通知
* 方法正常执行后的通知。
* 在配置文件中编写具体的配置:<aop:after-returning method="afterReturning" pointcut-ref="myPointcut2"/>
* 应用:可以修改方法的返回值
4. 异常抛出通知
* 在抛出异常后通知
* 在配置文件中编写具体的配置:<aop:after-throwing method="afterThorwing" pointcut-ref="myPointcut3"/>
* 应用:包装异常的信息
5. 环绕通知
* 方法的执行前后执行。
* 在配置文件中编写具体的配置:<aop:around method="around" pointcut-ref="myPointcut2"/>
* 要注意:目标的方法默认不执行,需要使用ProceedingJoinPoint对来让目标对象的方法执行。
public void around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("方法执行之前...");
// 让目标对象的方法执行,让save方法执行
joinPoint.proceed();
System.out.println("方法执行之后...");
}