- 依赖的jar
如果你的项目使用Maven构建,引入这个依赖即可。
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>org.aspectj-library</artifactId>
<type>libd</type>
<version>1.7.1.RELEASE</version>
</dependency>
如果是普通的项目,可以到spring中的lib/aspect中引入aspectjrt.jar、aspectjweaver.jar和lib/asm中asm-2.2.3.jar、asm-commons-2.2.3.jar、asm-util-2.2.3.jar
2.在配置文件中引入AOP
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy><!-- 引入AOP -->
</beans>
3.实际使用例子
package com.wtkj.jsxt.qfgl.log;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
/**
*
* 利用AOP为业务层生成数据模块的三层添加log。
* @author changsheng
*/
@Aspect
@Component
public class LoggingOperation {
static final Log log = LogFactory.getLog(LoggingOperation.class);
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
@Pointcut("execution( * com.wtkj.jsxt.qfgl.web.CreateDataController.*(..)) || execution( * com.wtkj.jsxt.qfgl.business.impl.CreateDataBusinessImpl.*(..)) || execution( * com.wtkj.jsxt.qfgl.data.impl.CreateDataDAOImpl.*(..))")
public void logging() {
}
@Around("logging()")
public Object before(ProceedingJoinPoint pjp) throws Exception {
StopWatch sw = new StopWatch();
StringBuilder sb = new StringBuilder();
sb.append(pjp.getTarget().getClass().getName() + "."
+ pjp.getSignature().getName());
sb.append(" : parameters = [");
Object[] args = pjp.getArgs();
for (int i = 0; i < args.length; i++) {
sb.append(args[i].getClass().getName());
sb.append(" = ");
if (args[i] instanceof Calendar) {
Calendar cal = (Calendar) args[i];
sb.append(sdf.format(cal.getTime()));
} else if(args[i] instanceof Integer){
sb.append(args[i].toString());
} else {
log.info("NotProcessType = "+args[i].getClass());
}
if (i != args.length - 1)
sb.append(" , ");
}
sb.append(" ] , ");
sw.start();
Object result = null;
try {
result = pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
sw.stop();
sb.append(" ReturnValue = ");
sb.append(result.toString());
sb.append(" , ");
double totalTime = sw.getTotalTimeSeconds();
sb.append(" TotalTime = " + totalTime + "s ");
log.info(sb.toString());
return result;
}
@AfterThrowing(pointcut="logging()",throwing="e")
public void logException(Throwable e) {
log.info(e.getStackTrace());
}
}
利用切入点表达式,定位到需要日志的方法,打印出输入参数,结果参数,耗时等相关信息。
4.以上的例子只是个示例,实际在应用当中应该很灵活的应用AOP。比如可以自定义一个注解用来记录业务信息,@BusinessLog("增加员工"),然后利用切入点表达式,定位到相应注解,来记录业务日志。
5.可以利用AOP在DAO层进行缓存处理,可以利用AOP对系统日志进行管理,可以利用AOP对系统性能进行监控(譬如监控耗时最大的一些方法)等。