spring boot空属性赋值问题+aspect日志
2020-08-03 20:56
1781 查看
空属性赋值问题
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 @Component public 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 pojo 对象日期属性问题
- @Aspect统一处理Web请求日志--Spring Boot--Java EE开发,原来可以这样!
- 为什么Spring Boot推荐使用logback-spring.xml来替代logback.xml来配置logback日志的问题分析
- springboot2.x+log4j2(springboot2.x整合log4j2日志日志 包冲突问题解决)
- Spring Boot如何让Web API自动生成文档,并解决swagger-annotations的API注解description属性废弃的问题
- springboot问题,没有主清单属性
- 13.7 SpringBoot集成日志系统logback的几个问题
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式)
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式+注解Log)
- [求助]关于springboot属性注入的问题,如图
- springboot问题,没有主清单属性
- springboot+maven打jar包运行java -jar,报错没有主清单属性;pom文件添加build修正问题
- 解决spring-boot项目中无法读取yml配置文件属性问题
- SpringBoot@RequestBody无法获取页面传递的参数,属性名命名规范问题
- 关于Spring boot中读取属性配置文件出现中文乱码的问题的解决
- Spring Boot(十一)使用AOP,@Aspect统一处理Web请求日志
- Idea打包springboot项目jar包,运行报找不到主清单属性或找不到主类问题解决方法。
- idea下springboot工程 @Autowired注解属性报红编译不通过及部分相关问题的解决办法
- springboot整合mongodb时debug模式下日志一直刷新问题
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)