Spring AOP
代码流程
1、aop配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 扫包 -->
<context:component-scan base-package="com.eastwit.**"></context:component-scan>
<!-- 开启AOP -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
.........................................................................................................
2、自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@Component
public @interface AppTraceLog {
String model() default "";
AppTrace APP_TRACE() default AppTrace.DEFAULT;
}
3、配置切面类
@Aspect
@Component
public class AppTraceAspect {
private static final Logger LOGGER = LogManager.getLogger(AppTraceAspect.class);
@Value("#{properties.XXX}")
private String XXX;
@Value("#{properties.YYY}")
private String YYY;
@Resource(name = "behaviourForeignServiceImpl")
private IBehaviourForeignService behaviourForeignService;
@AfterReturning(pointcut = "within(com.eastwit..*)&&@annotation(rl)" , returning = "res")
public void addLogSuccess(JoinPoint jp, AppTraceLog rl,Object res) {
try {
AppBehavior appBehavior = getParams();
appBehavior.setLogtype(rl.APP_TRACE().getCode());
appBehavior.setModule(rl.model());
getParamsIn(jp,appBehavior);
getResponse(res , appBehavior);
behaviourForeignService.inserAppInfo(appBehavior);
}catch (Exception e){
LOGGER.error("dingzhi error,ignore it", e);
}
}
private void getResponse(Object res, AppBehavior appBehavior) {
JSONArray jsonArray = new JSONArray();
try {
JSONObject jsonObject = new JSONObject((Map)res);
jsonArray.add(jsonObject);
}catch (Exception e){
ResponseData res1 = (ResponseData) res;
JSONObject jsonObject = new JSONObject((Map)res1.getData());
jsonArray.add(jsonObject);
LOGGER.info("getResponse faild :" + e);
}
appBehavior.setResponse(String.valueOf(jsonArray));
appBehavior.setResponsetype("1");
appBehavior.setResult("成功");
appBehavior.setErrorcode("");
appBehavior.setErrorlog("");
appBehavior.setContent("");
appBehavior.setCardno(XXX);
appBehavior.setSource(YYY);
}
private void getParamsIn(JoinPoint joinPoint, AppBehavior appBehavior){
try {
JSONObject param = new JSONObject();
StringBuffer ysParam = new StringBuffer();
Object[] paramValues = joinPoint.getArgs();
String[] paramNames = ((CodeSignature)joinPoint.getSignature()).getParameterNames();
for (int i = 0; i < paramNames.length; i++) {
if (!(paramValues[i] instanceof HttpServletRequest) && !(paramValues[i] instanceof HttpServletResponse)) {
param.put(paramNames[i], paramValues[i]);
ysParam.append(paramNames[i] + "=" + paramValues[i]);
ysParam.append(",");
}
}
appBehavior.setParams(ysParam.length() == 0 ? "" : ysParam.substring(0, ysParam.length()-1));
appBehavior.setFormatparam(param.toJSONString());
}catch (Exception e){
LOGGER.info("getParamsIn faild :" + e);
}
}
private AppBehavior getParams() {
try {
AppBehavior appBehavior = new AppBehavior();
appBehavior.setPoliceid("");
appBehavior.setSn("");
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
String networkProtocol = request.getScheme();
String localAddr = request.getLocalAddr();
int serverPort = request.getServerPort();
HttpSession session = request.getSession();
appBehavior.setRequestid(getUUID32());
appBehavior.setSessionid(session.getId());
appBehavior.setTerminalip(request.getRemoteAddr());
appBehavior.setSourceip(request.getRemoteAddr());
appBehavior.setSourceport(request.getRemotePort());
appBehavior.setDestip(localAddr);
appBehavior.setDestport(serverPort);
appBehavior.setTime(BigInteger.valueOf(System.currentTimeMillis()));
appBehavior.setUri(request.getRequestURI());
appBehavior.setUrl(networkProtocol + "://" + localAddr + ":" + serverPort + request.getRequestURI());
return appBehavior;
} catch (Exception e) {
LOGGER.info("getParams faild :" + e);
}
return null;
}
}
4、类方法添加注解
@AppTraceLog(model = "工单 > 今日告警及工单",APP_TRACE = AppTrace.LOGIN)
@RequestMapping("/inquireIndexMessage")
@ResponseBody
public Object inquireIndexMessage(HttpServletRequest request, HttpServletResponse response) {
}
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bcc572433c3fab13bc55600e80d9c6d5.png)