项目迭代之:空属性赋值问题、aspect日志
- 空属性赋值
- aspect日志
空属性赋值
一、在Util里新建MyBeanUtils
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修改
@Overridepublic 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中对其调用
public static void copyProperties(java.lang.Object source, java.lang.Object target, java.lang.String... ignoreProperties)throws org.springframework.beans.BeansException { /* compiled code */ }
BeanUtils.copyProperties(news,news1, MyBeanUtils.getNullPropertyNames(news));
aspect日志
在news中新建aspect包,在这里新建LogAspect类
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@Componentpublic 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){//获取requestServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request=attributes.getRequest();//获取url和ipString 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;}@Overridepublic String toString() {return \"RequestLog{\" +\"url=\'\" + url + \'\\\'\' +\", ip=\'\" + ip + \'\\\'\' +\", classMethod=\'\" + classMethod + \'\\\'\' +\", args=\" + Arrays.toString(args) +\'}\';}}}