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. IDEA 程序包不存在,找不到符号但是明明存在对应的jar包(问题分析及解决方案)2. Java实现Windows计算器界面3. python与pycharm有何区别4. Java多线程通信wait()和notify()代码实例5. IDEA 2020.1 版自动导入MAVEN依赖的方法(新版MAVEN无法自动导入/更新POM依赖、MAVEN设置自动更新、自动更新快捷键)6. 开发Java的又一利器-Visual Cafe7. Java中volatile防止指令重排8. 解决vue watch数据的方法被调用了两次的问题9. javascript将扁平的数据转为树形结构的高效率算法10. IDEA标签tabs多行显示的设置
![IDEA 程序包不存在,找不到符号但是明明存在对应的jar包(问题分析及解决方案)](http://www.haobala.com/attached/image/news/202309/1051516506.png)