配置文件:application_context.xml
<!-- aop注解 proxy-target-class="true" 切入的类可以是代理类 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!--切面-->
<bean id="systemLogAspect" class="com.lty.common.log.SystemLogAspect"></bean>
java 代码:
package com.lty.common.log;
import javax.annotation.Resource;
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.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.lty.common.redis.RedisHelper;
import com.lty.common.util.Constants;
import com.lty.thailand.entity.BaseUserLog;
import com.lty.thailand.service.impl.BaseUserLogService;
/**
* @desc 切点类
*/
@Aspect
@Component
public class SystemLogAspect {
//注入Service用于把日志保存数据库
@Resource //这里我用resource注解,一般用的是@Autowired,他们的区别如有时间我会在后面的博客中来写
private BaseUserLogService baseUserLogService;
//
private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class);
@Pointcut("execution (* com.lty.travel.controller.ManagerController.*(..))")
public void controllerPointcut() {}
@Around("controllerPointcut()")
public Object controllerAround(ProceedingJoinPoint joinPoint) throws Throwable { // 执行的类
String exeType = null; // 执行的方法
String exeMethod = null;
Object retObj = null ;
long start = System.currentTimeMillis();
try { // 获取接口的请求参数
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
exeType = signature.getDeclaringType().getCanonicalName();
exeMethod = signature.getMethod().getName();
BaseUserLog baseUserLog = null ;
String sqlId = null ;
boolean needInsert = false;
if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {
for ( int i = 0; i < joinPoint.getArgs().length; i++) {
baseUserLog = new BaseUserLog();
JSONObject requestParam = JSONObject.parseObject((String)joinPoint.getArgs()[i]);
String userId = requestParam.getString("userId");
if(!StringUtils.isEmpty(userId)){
needInsert = true ;
baseUserLog.setUserId(userId);
sqlId = requestParam.getString("sqlId");
baseUserLog.setOperType(sqlId);
}
}
}
try{
retObj = joinPoint.proceed();
if("getCertification".equals(sqlId)||"getCertificationNew".equals(sqlId)){
JSONObject requestParam = JSONObject.parseObject((String)retObj);
String userIdss = requestParam.getString("data");
JSONObject requestParam22 = JSONObject.parseObject(userIdss);
baseUserLog.setUserId(requestParam22.getString("userId"));
}
baseUserLog.setStatus(0);
}catch(Exception e){
baseUserLog.setStatus(1);
}
if(needInsert){
baseUserLog.setExecuteTime(System.currentTimeMillis() - start);
if(!"quitApp".equals(sqlId)){
RedisHelper.setWithExpireTime(Constants.USER_KEY+""+baseUserLog.getUserId(), baseUserLog.getUserId(), 30*60);
}
baseUserLogService.insertBaseUserLog(baseUserLog);
}
} catch (Throwable ex) {
} finally {
logger.info("[{}.{} end,cost {} ms]", new Object[] { exeType, exeMethod, (System.currentTimeMillis() - start)
});
}
return retObj;
}
}
访问静态图片
配置文件:application_context.xml
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类逻辑 ->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>