spring aop

1、编写pojo 添加注解
   
  1. package com.netfinworks.fax.admin.web.aop;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.annotation.Aspect;
  4. import org.aspectj.lang.annotation.Before;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. import com.netfinworks.invest.request.QueryRequest;
  7. /**
  8. * <p>分页参数设置</p>
  9. * @author Your name
  10. * @version $Id: PaginationAOP.java, v 0.1 2015年5月22日 下午2:43:39 weichunhe Exp $
  11. */
  12. @Aspect
  13. public class PaginationAOP {
  14. private int pageSize;
  15. private int pageIndex;
  16. public int getPageSize() {
  17. return pageSize;
  18. }
  19. public void setPageSize(int pageSize) {
  20. this.pageSize = pageSize;
  21. }
  22. public int getPageIndex() {
  23. return pageIndex;
  24. }
  25. public void setPageIndex(int pageIndex) {
  26. this.pageIndex = pageIndex;
  27. }
  28. @Pointcut("execution(* com.netfinworks.fax.admin.web.action.general.*.enter*(..))")
  29. public void pointcut(){
  30. }
  31. @Before("pointcut()")
  32. public void enter(JoinPoint jp){
  33. Object[] args = jp.getArgs();
  34. QueryRequest paginaReq = null;
  35. //找到请求参数
  36. for (Object object : args) {
  37. if (object instanceof QueryRequest) {
  38. paginaReq = (QueryRequest) object;
  39. break;
  40. }
  41. }
  42. if (paginaReq.getPageNum() == null || paginaReq.getPageNum() == 0) {
  43. paginaReq.setPageNum(pageIndex);
  44. }
  45. if (paginaReq.getPageSize() == null || paginaReq.getPageSize() == 0) {
  46. paginaReq.setPageSize(pageSize);
  47. }
  48. }
  49. }
2、配置文件
  1. <aop:aspectj-autoproxy proxy-target-class="true"/>
  2. <!-- 分页参数aop -->
  3. <bean id="paginationAop" class="com.netfinworks.fax.admin.web.aop.PaginationAOP">
  4. <property name="pageSize" value="15"></property>
  5. <property name="pageIndex" value="1"></property>
  6. </bean>

 

can't find referenced pointcut pointcut

解决的办法就是下载最新的aspectjrt的jar包即可

controller 被 aop 切点包含之后,就映射不成请求了 (spring mvc  通过requestMap 注解就失效了)
定义mvc的配置文件,一般是 <<servlet name>>-servlet.xml,一般(也是推荐做法)使用auto scan来定义所有的controller.关键步骤来了:这个文件也要加入<aop:aspectj-autoproxy proxy-target-class="true"/>, 一定要添加proxy-target-class="true"! 这是用于通知spring使用cglib而不是jdk的来生成代理方法。 
 





posted @ 2015-10-12 15:11  skyding  阅读(238)  评论(0)    收藏  举报