空属性赋值问题+aspect日志

文章目录

    • 1.空属性赋值问题
    • 2.aspect日志
    • 3.运行结果

1.空属性赋值问题

1.建立自己的工具类,使得copy对象时,把值为空的变量不copy过去

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()]);
    }
}

2.在NewServiceImpl中对updateNew方法进行修改
空属性赋值问题+aspect日志_第1张图片

2.aspect日志

1.新建一个LogAspect类

@Aspect
@Component
public class LogAspect {
     

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

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

    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
     
        //获取request
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        logger.info("request:{}",request);
        logger.info("----------------doBefore-------------");

    }

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

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

2.在LogAspect类中建立Inner class,封装需要打印的信息

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) +
                    '}';
        }
    }

3.修改打印log,用Inner class的变量

    @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 ip = request.getRemoteAddr();
        String classMethod = joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        RequestLog requestLog = new RequestLog(url,ip,classMethod,args);
        logger.info("Request:{}",requestLog);
        logger.info("----------------doBefore-------------");

    }

3.运行结果

1.空属性赋值
在这里插入图片描述在这里插入图片描述2.日志
在这里插入图片描述

你可能感兴趣的:(java,spring,boot)