首先当然是定义一个注解类型了:
/**
*
* 日志切面注解
* @Description: TODO <p>MethodLog.java</p>
* @作者: 王彦宝
* @时间: 2018年8月24日上午10:08:08
* @version V1.0
* @see java.lang.Class
* @since JDK{jdk1.7}
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodLog {
String content() default "";
MethodLogType operType() default MethodLogType.QUERY;
}
在这个注解中我定义了两个参数:一个是content:这个是日志的主要内容,一个是operType : 日志类型。
其中MethodLogType 是我自己定义的一个枚举类:
/**
* methodLog注解参数 operType 的枚举值
*
* @Description: TODO <p>MethodLogType.java</p>
* @作者: 王彦宝
* @时间: 2018年8月24日下午2:21:57
* @version V1.0
* @see java.lang.Class
* @since JDK{jdk1.7}
*/
public enum MethodLogType {
/* 查询 */
QUERY,
/* 新增 */
ADD,
/* 修改 */
UPDATE,
/*导出*/
EXPORTS,
/*导入*/
IMPORTS,
/* 删除 */
DELETE;
}
下面就要通过spring 的aop 实现业务日志插入数据库了:
@Component
@Aspect //
public class LogService {
@Resource(name = "SaveLogDao")
private SaveLogDao dao;
public LogService() {
}
/**
* 切点
*/
@Pointcut("@annotation(com.eimageglobal.iq.client.util.MethodLog)")
public void methodCachePointcut() { }
/**
* 切面
*
* @param point
* @return
* @throws Throwable
*/
@Around("methodCachePointcut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
Calendar ca = Calendar.getInstance();
String operDate = df.format(ca.getTime());
String methodRemark = getMthodRemark(point);
String methodOpertype = getMthodOperType(point);
String methodName = point.getSignature().getName();
String packages = point.getThis().getClass().getName();
if (packages.indexOf("$$EnhancerByCGLIB$$") > -1) { // 如果是CGLIB动态生成的类
try {
packages = packages.substring(0, packages.indexOf("$$"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
String operatingcontent = "";
Object[] method_param = null;
Object object=null;
try {
method_param = point.getArgs(); //获取方法参数
dao.save(dolog);
String param=(String) point.proceed(point.getArgs());
// object = point.proceed();
} catch (Exception e) {
// 异常处理记录日志..log.error(e);
e.printStackTrace();
}
return object;
}
/**
* 方法异常时调用
*
* @param ex
*/
public void afterThrowing(Exception ex) {
System.out.println("afterThrowing");
System.out.println(ex);
}
/**
* 获取方法中的中文备注
*
* @param joinPoint
* @return
* @throws Exception
*/
public static String getMthodRemark(ProceedingJoinPoint joinPoint) throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] method = targetClass.getMethods();
String methode = "";
for (Method m : method) {
if (m.getName().equals(methodName)) {
Class[] tmpCs = m.getParameterTypes();
if (tmpCs.length == arguments.length) {
MethodLog methodCache = m.getAnnotation(MethodLog.class);
if (methodCache != null) {
methode = methodCache.remark();
}
break;
}
}
}
return methode;
}
/**
* 获取方法中的中文备注
*
* @param joinPoint
* @return
* @throws Exception
*/
public static String getMthodOperType(ProceedingJoinPoint joinPoint) throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] method = targetClass.getMethods();
MethodLogType methode = null;
String type="";
for (Method m : method) {
if (m.getName().equals(methodName)) {
Class[] tmpCs = m.getParameterTypes();
if (tmpCs.length == arguments.length) {
MethodLog methodCache = m.getAnnotation(MethodLog.class);
if (methodCache != null) {
methode = methodCache.operType();
}
break;
}
}
}
return type=LogType.getName(methode.toString());
}
在spring -source.xml 中通过bean 注入 :
<bean id="SaveLogDao" class="com.eimageglobal.iq.biz.dao.impl.SaveLogDaoImpl"/>
这里的SaveLogDao 就是 LogService 中
@Resource(name = "SaveLogDao")
private SaveLogDao dao;
这里的用到。
在业务代码中的使用样例:
/**
*
* @param request
* @param response
* @作者: 王彦宝
* @时间: 2018年8月24日下午4:00:55
* @返回 void
*/
@RequestMapping(value = "XXXXXXXXX")
@MethodLog(content="这里是你要插入的日志内容",operType=MethodLogType.UPDATE)
public void updateSort(HttpServletRequest request, HttpServletResponse response){
}
这样在你需要插入日志的地方加这样一个注解就ok 了