spring boot空屬性賦值問題與aspect日志實(shí)現(xiàn)方法
空屬性賦值問題
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中對(duì)updateNew方法進(jìn)行修改
@Override public News updateNew(Long id, News news) { News news1=newRepository.findById(id).orElse(null); if(news1==null){ // System.out.println('未獲得更新對(duì)象'); throw new NotFoundException('該新聞不存在'); } //更新后傳入的news復(fù)制給news1,查找更新數(shù)據(jù)news中空值屬性,忽略不復(fù)制給news1 BeanUtils.copyProperties(news,news1, MyBeanUtils.getNullPropertyNames(news)); news1.setUpdateTime(new Date()); return newRepository.save(news1); }
日志打印
新建一個(gè)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) + ’}’; } }}
效果展示
總結(jié)
到此這篇關(guān)于spring boot空屬性賦值問題與aspect日志實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)spring boot空屬性賦值內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 詳解CSS開發(fā)過程中的20個(gè)快速提升技巧2. 一篇文章帶你了解JavaScript-語(yǔ)句3. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟4. 常見 PHP ORM 框架與簡(jiǎn)單代碼實(shí)現(xiàn)5. asp讀取xml文件和記數(shù)6. XML 取得元素的字符數(shù)據(jù)7. Ajax實(shí)現(xiàn)動(dòng)態(tài)顯示并操作表信息的方法8. 如何使用ASP.NET Core 配置文件9. .Net core 的熱插拔機(jī)制的深入探索及卸載問題求救指南10. ASP+ajax實(shí)現(xiàn)頂一下、踩一下同支持與反對(duì)的實(shí)現(xiàn)代碼
