1、编写pojo 添加注解
package com.netfinworks.fax.admin.web.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import com.netfinworks.invest.request.QueryRequest;/*** <p>分页参数设置</p>* @author Your name* @version $Id: PaginationAOP.java, v 0.1 2015年5月22日 下午2:43:39 weichunhe Exp $*/@Aspectpublic class PaginationAOP {private int pageSize;private int pageIndex;public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getPageIndex() {return pageIndex;}public void setPageIndex(int pageIndex) {this.pageIndex = pageIndex;}@Pointcut("execution(* com.netfinworks.fax.admin.web.action.general.*.enter*(..))")public void pointcut(){}@Before("pointcut()")public void enter(JoinPoint jp){Object[] args = jp.getArgs();QueryRequest paginaReq = null;//找到请求参数for (Object object : args) {if (object instanceof QueryRequest) {paginaReq = (QueryRequest) object;break;}}if (paginaReq.getPageNum() == null || paginaReq.getPageNum() == 0) {paginaReq.setPageNum(pageIndex);}if (paginaReq.getPageSize() == null || paginaReq.getPageSize() == 0) {paginaReq.setPageSize(pageSize);}}}
2、配置文件
<aop:aspectj-autoproxy proxy-target-class="true"/><!-- 分页参数aop --><bean id="paginationAop" class="com.netfinworks.fax.admin.web.aop.PaginationAOP"><property name="pageSize" value="15"></property><property name="pageIndex" value="1"></property></bean>
解决的办法就是下载最新的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的来生成代理方法。