Spring之AOP配置
2024-09-01 19:51:38
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/
1、什么是AOP?
AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程,当前已经成为一种比较成熟的编程思想,可以用来很好的解决应用系统中分布于各个模块的交叉关注点问题。在轻量级的J2EE中应用开发中,使用AOP来灵活处理一些具有横切性质的系统级服务,如系统日志、异常处理、事务处理、安全检查、缓存、对象池管理等,已经成为一种非常适用的解决方案。 AOP中比较重要的概念有:Aspect、JoinPoint、PonitCut、Advice、Introduction、Weave、Target Object、Proxy Object等。
本文主要用来做接入层日志收集,接入层异常统一处理,以及后续性能分析。
2、如何使用AOP?
package com.mao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; import com.mao.util.JsonUtil; /**
*
* 项目名称:
* 模块名称:
* 功能描述:
* 创建人: mao2080@sina.com
* 创建时间:2017年5月3日 下午6:34:37
* 修改人: mao2080@sina.com
* 修改时间:2017年5月3日 下午6:34:37
*/
@Aspect
@Component
public class LogerInterceptor { /**日志*/
private static final Log logger = LogFactory.getLog(LogerInterceptor.class); /**
*
* 描述:切入点(Controller层)
* @author mao2080@sina.com
* @created 2017年5月3日 下午4:49:47
* @since
*/
@Pointcut("execution(public * com.web.controller.*.*(..))")
public void controllerAround() { } /**
*
* 描述:执行切面环绕(Controller层)
* @author mao2080@sina.com
* @created 2017年5月3日 下午4:52:03
* @since
* @param joinPoint 切入点上下文,包含目标方法和参数等
* @return 目标方法执行的结果
* @throws Throwable
*/
@Around("controllerAround()")
public Object doControllerAround(ProceedingJoinPoint joinPoint) throws Throwable {
//获取类名
String className = joinPoint.getSignature().toShortString();
//获取入参
Object inputArgs = joinPoint.getArgs();
//入参日志
logger.info(className+" input:"+JsonUtil.toJson(inputArgs));
//记录时间
long sta = System.currentTimeMillis();
//调用方法
Object result = null;
try {
result = joinPoint.proceed();
} catch (Exception e) {
logger.error(className+" exception: "+e.toString()+", input: "+JsonUtil.toJson(inputArgs));
result = new ResObject(201, "系统出现异常"+e.getMessage());
}
//出参日志
logger.info(className+" output:"+JsonUtil.toJson(result)+", execute time:"+(System.currentTimeMillis() - sta)+"ms");
return result;
} }
3、ResObject
package com.mao; import java.io.Serializable; /**
*
* 项目名称:
* 模块名称:
* 功能描述:
* 创建人: mao2080@sina.com
* 创建时间:2017年5月3日 下午6:37:11
* 修改人: mao2080@sina.com
* 修改时间:2017年5月3日 下午6:37:11
*/
public class ResObject implements Serializable{ /**序列号*/
private static final long serialVersionUID = 589903502110209046L; /**返回代码*/
private int code = 200; /**返回信息*/
private String desc = "Success."; /**返回数据*/
private Object data; /**
*
* 构建函数
* @author mao2080@sina.com
* @created 2017年3月24日 下午4:25:23
* @since
*/
public ResObject() { } /**
*
* 描述:构造函数
* @author mao2080@sina.com
* @created 2017年4月18日 下午3:32:26
* @since
* @param data 数据
*/
public ResObject(Object data) {
super();
this.data = data;
} /**
*
* 构建函数
* @author mao2080@sina.com
* @created 2017年3月24日 下午4:25:35
* @since
* @param code 返回代码
* @param desc 返回信息
*/
public ResObject(int code, String desc) {
super();
this.code = code;
this.desc = desc;
} /**
*
* 构建函数
* @author mao2080@sina.com
* @created 2017年3月24日 下午4:25:39
* @since
* @param code 返回代码
* @param desc 返回信息
* @param data 返回数据
*/
public ResObject(int code, String desc, Object data) {
super();
this.code = code;
this.desc = desc;
this.data = data;
} public Object getData() {
return data;
} public void setData(Object data) {
this.data = data;
} public int getCode() {
return code;
} public void setCode(int code) {
this.code = code;
} public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} }
4、访问结果
2017-05-03 18:41:51,633 (http-bio-8080-exec-4) [INFO - com.mao.LogerInterceptor.doControllerAround(LogerInterceptor.java:58)] LoginController.login(..) input:["mao","123456"]
2017-05-03 18:41:51,737 (http-bio-8080-exec-4) [INFO - com.mao.LogerInterceptor.doControllerAround(LogerInterceptor.java:70)] LoginController.login(..) output:{"code":200,"data":{"token":"64501f456ec667d420ac2610b4fd81e1",},"desc":"Success."}, execute time:103ms
5、配置注意事项
由于使用了springmvc,导致配置spring aop时没有起作用,最后需要在spring配置文件中加入这项配置。
<aop:aspectj-autoproxy proxy-target-class="false"/>
6、参考网站
http://blog.csdn.net/luoshenfu001/article/details/5816408/
http://outofmemory.cn/code-snippet/3025/spring-AOP-Around-Before-After-differentiate
最新文章
- spring类型自动转换——@InitBinder和Converter
- android的程序运行数据存放在哪里?
- IBM Domino 9 出现 Domino Designer 您正在试图升级多用户安装。请获取正确的安装包以完成升级。 解决方案
- UESTC 395 Dynamic Query System --Treap
- C#零碎知识汇总
- android发送/接收json数据
- json对象和字符串互相转换
- Social networks and health: Communicable but not infectious
- PHP内核之旅-3.变量
- anaconda 命令集合
- canvas 实现刮刮乐
- (后端)mybatis 模糊查询 mapper.xml的写法(转)
- python3.7之12306抢票脚本实现
- linux下混杂模式
- “failed to excute script xxx” PyInstaller 打包python程序为exe文件过程错误
- zk会话,快照,序列化,本地存储
- Linux学习笔记03—初识Linux
- UIProgressView 详解
- where are you from
- POJ2777(线段树裸题)