spring boot空屬性賦值問題與aspect日志實現方法
空屬性賦值問題
MyBeanUtils類
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方法進行修改
@Override public News updateNew(Long id, News news) { News news1=newRepository.findById(id).orElse(null); if(news1==null){ // System.out.println('未獲得更新對象'); throw new NotFoundException('該新聞不存在'); } //更新后傳入的news復制給news1,查找更新數據news中空值屬性,忽略不復制給news1 BeanUtils.copyProperties(news,news1, MyBeanUtils.getNullPropertyNames(news)); news1.setUpdateTime(new Date()); return newRepository.save(news1); }
日志打印
新建一個LogAspect類
@Aspect@Componentpublic class LogAspect { 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 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('_____________________doBefore_______________________'); } @After('log()') public void doAfter(){ logger.info('_____________________doAfter_______________________'); } @AfterReturning(returning = 'result',pointcut = 'log()') public void adAfterReturn(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 className, Object[] args) { this.url = url; this.ip = ip; this.classMethod = className; this.args = args; } @Override public String toString() { return 'Requestlog{' + 'url=’' + url + ’’’ + ', ip=’' + ip + ’’’ + ', classMethod=’' + classMethod + ’’’ + ', args=' + Arrays.toString(args) + ’}’; } }}
效果展示
總結
到此這篇關于spring boot空屬性賦值問題與aspect日志實現方法的文章就介紹到這了,更多相關spring boot空屬性賦值內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. 將properties文件的配置設置為整個Web應用的全局變量實現方法2. 一文秒懂idea的git插件跟翻譯插件3. docker compose idea CreateProcess error=2 系統找不到指定的文件的問題4. XML入門的常見問題(四)5. python中pandas.read_csv()函數的深入講解6. Python通過format函數格式化顯示值7. layui Ajax請求給下拉框賦值的實例8. JS中的常見數組遍歷案例詳解(forEach, map, filter, sort, reduce, every)9. JS算法題解旋轉數組方法示例10. python爬蟲利用代理池更換IP的方法步驟
