您的位置:首页 > 编程语言 > Java开发

spring aop

2015-10-12 15:11 239 查看
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 $

*/

@Aspect

public 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);

}

}

}

[/code]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>

[/code]

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的来生成代理方法。
来源: <http://usherlight.iteye.com/blog/1306111>

来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: