您的位置:首页 > 编程语言 > ASP

Springboot项目-空属性赋值问题+aspect日志

2020-08-04 17:00 696 查看 https://blog.csdn.net/weixin_4

1.空属性赋值问题

一、在Util里新建MyBeanUtils

[code]package com.whut.news.util;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;

public class MyBeanUtils {

public static String[] getNullPropertyNames(Object source){
BeanWrapper beanWrapper=new BeanWrapperImpl(source);
PropertyDescriptor[] pds=beanWrapper.getPropertyDescriptors();
List<String> nullPropertyNames=new ArrayList<>();
for (PropertyDescriptor pd:pds){
String propertyName=pd.getName();
if(beanWrapper.getPropertyValue(propertyName)==null){
nullPropertyNames.add(propertyName);
}
}
return nullPropertyNames.toArray(new String[nullPropertyNames.size()]);
}

}

二、NewServiceImpl.updateNew修改

[code]@Override
public News updateNew(Long id, News news) {
News news1=newRepository.findById(id).orElse(null);
if(news1==null){
throw new NotFoundException("该新闻不存在");
}
BeanUtils.copyProperties(news,news1, MyBeanUtils.getNullPropertyNames(news));
news1.setUpdateTime(new Date());
return newRepository.save(news1);
}

三、修改copyProperties与updateNew中对其调用

[code] public static void copyProperties(java.lang.Object source, java.lang.Object target, java.lang.String... ignoreProperties)
throws org.springframework.beans.BeansException { /* compiled code */ }
[code]BeanUtils.copyProperties(news,news1, MyBeanUtils.getNullPropertyNames(news));

2.aspect日志

在news中新建aspect包,在这里新建LogAspect类

[code]package com.whut.news.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
@Component
public class LoginAspect {

private final Logger logger= LoggerFactory.getLogger(this.getClass());

@Pointcut("execution(* com.zr0726.news.web.*.*(..))")
public void log() {
}

@Before("log()")
public void doBefore(JoinPoint joinPoint){

//获取request
ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request=attributes.getRequest();
//获取url和ip
String url=request.getRequestURL().toString();
String classMethod=joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName();
String ip=request.getRemoteAddr();
Object[] args=joinPoint.getArgs();
RequestLog requestLog=new RequestLog(url,classMethod,ip,args);
logger.info("Request:{}",requestLog);
logger.info("-------------------doBefore--------------------");
}

@After("log()")
public void doAfter(){
logger.info("-------------------doAfter---------------------");
}

@AfterReturning(returning = "result",pointcut = "log()")
public void doAfterReturn(Object result){
logger.info("Result:{}",result);
}

private class RequestLog{
private String url;
private String ip;
private String classMethod;
private Object[] args;

public RequestLog(String url, String ip, String classMethod, Object[] args) {
this.url = url;
this.ip = ip;
this.classMethod = classMethod;
this.args = args;
}

@Override
public String toString() {
return "RequestLog{" +
"url='" + url + '\'' +
", ip='" + ip + '\'' +
", classMethod='" + classMethod + '\'' +
", args=" + Arrays.toString(args) +
'}';
}
}
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: