1 在spring-mvc.xml中,配置信息
<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
2 添加切面
import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.nufront.euht.service.web.service.WebSystemLogService;
import com.nufront.euht.web.common.util.MyPropertiesUtil;
/**
* 系统日志AOP切面
* */
@Component
@Aspect
public class SystemLogAspect {
@Autowired
private WebSystemLogService webSystemLogService;
//设置切入点,匹配所有controller的所有方法
public static final String CONTROLLER_PONINT_CUT="execution(* com.nufront.euht.controller..*.*(..))";
@After(CONTROLLER_PONINT_CUT)
public void after(JoinPoint joinpoint){
// 解析请求信息
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String userName = (String) SecurityUtils.getSubject().getPrincipal();
String ip = request.getRemoteAddr();
String url = request.getRequestURI();
Map parameter= request.getParameterMap();
String paramStr = this.parseMap(parameter);
Timestamp time = new Timestamp(new Date().getTime());
String resourceName = MyPropertiesUtil.getProp("systemLog.properties", url);
//将日志保存在数据库中.
SystemLog systemLog = new SystemLog(resourceName, url, paramStr, userName, ip, time);
webSystemLogService.addOneSystemLog(systemLog);
}
private String parseMap(Map parameter){
StringBuffer stringBuffer = new StringBuffer();
Iterator<Map.Entry<Object, Object>> it = parameter.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Object, Object> entry = it.next();
String key = entry.getKey().toString();
Object value = entry.getValue();
StringBuffer valSb = new StringBuffer();
if("[Ljava.lang.String".equals(value.toString().split(";")[0])){ //为数组
Object[] array = (Object[])value;
for(int i = 0 ; i <array.length ; i++ ){
valSb.append(array[i]+",");
}
String valSt = valSb.toString();
if(valSt.length() >= 1 ){
valSt = valSt.substring(0,valSt.length() -1);
}
stringBuffer.append("[key= " + key + " , value= {" + valSt+"}],");
}else{
stringBuffer.append("[key= " + key + " , value= " + value+"],");
}
}
String result = stringBuffer.toString();
if(result.length() >= 1 ){
result = result.substring(0,result.length() -1);
}
return result;
}
}
3 自动扫描切面
<context:component-scan base-package="com.nufront.euht.web.aop"/>
4 一个controller代码
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nufront.euht.model.CapEbu;
import com.nufront.euht.scanDevice.snmp.SnmpData;
import com.nufront.euht.service.web.charts.WebEbuService;
import com.nufront.euht.service.web.service.WebEbuLogService;
import com.nufront.euht.web.common.snmp.ParentSnmp;
import com.nufront.euht.web.common.util.MyTimeUtil;
import com.nufront.euht.web.deviceUpgrade.DeviceUpgradeThread;
import com.nufront.euht.web.ebu.getLog.model.EbuLog;
@Controller
@RequestMapping("/web/ebu/ebuLogController")
public class EbuLogController {
@Autowired
private WebEbuLogService webEbuLogService;
@Autowired
private WebEbuService webEbuService;
private final String Get_EBU_LOG_OID = "1.3.6.1.4.1.59418.1.1.1.1.2.1.13.0";
@RequestMapping("/forwardToEbuLog")
public String forwardToEbuLog (HttpServletRequest request,
HttpServletResponse response) throws Exception {
String ids = request.getParameter("ids");
request.setAttribute("ids",ids );
EbuLog ebuLog = this.webEbuLogService.getLastOneEbuLog();
request.setAttribute("ebuLog", ebuLog);
return "/web/ebu/getLog/getLog";
}
@RequestMapping("/getEbuLog")
public void getEbuLog (HttpServletRequest request,
HttpServletResponse response) throws Exception {
String ftpAddr = request.getParameter("ftpAddr");
String user = request.getParameter("userName");
String passwd = request.getParameter("passwd");
String file_src = request.getParameter("file_src");
String ids = request.getParameter("ids");
EbuLog ebuLogDB = this.webEbuLogService.getLastOneEbuLog();
boolean isChange = this.ebuLogIsChange(ftpAddr, user, passwd, file_src, ebuLogDB);
if(isChange){
EbuLog newEbuLog = this.createEbuLog(ftpAddr, user, passwd, file_src, request);
this.webEbuLogService.addOneEbuLog(newEbuLog);
ebuLogDB = newEbuLog;
}
String jsonResult = this.setEbuLogPDU(ids, ebuLogDB);
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.getWriter().write(jsonResult);
}
}