当前位置: 代码迷 >> 综合 >> Java开发学习.Day13
  详细解决方案

Java开发学习.Day13

热度:47   发布时间:2024-02-06 18:12:19.0

Java开发学习.Day13

  • 基于Springboot实现 空属性赋值问题、aspect日志
    • 空属性赋值问题
      • 代码实现
      • 效果展示
    • aspect日志
      • 代码实现
      • 效果展示

基于Springboot实现 空属性赋值问题、aspect日志

本文内容是基于Java开发学习.Day12内容上所实现的

空属性赋值问题

代码实现

在对表中对象进行更新操作时,为避免空属性替换原有数据,需要解决空属性赋值问题
创建MyBeanUtils类
其中getNullPropertyNames方法将获取对象所有空属性

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()]);}}

改写更新操作的具体方法

    @Overridepublic News updateNew(Long id, News news) {News news1 = newRepository.findById(id).orElse(null);if(news1==null){System.out.println("未获取更新对象");}BeanUtils.copyProperties(news,news1, MyBeanUtils.getNullPropertyNames(news));news1.setUpdateTime(new Date());return newRepository.save(news1);}

其中copyProperties方法将news中非空的其他属性存于news1中,再将news1写入数据库,即可解决空属性赋值问题
以下是copyProperties的源码

    public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {copyProperties(source, target, (Class)null, ignoreProperties);}

效果展示

create_time未被空属性覆盖为null
在这里插入图片描述

aspect日志

代码实现

创建LogAspect类实现切面
将com.xxxxbt.news.web下所有类,所有的方法

@Aspect   
@Component
public class LogAspect {private final Logger logger = LoggerFactory.getLogger(this.getClass());@Pointcut("execution(* com.xxxxbt.news.web.*.*(..))")   // 切入点public void log(){}@Before("log()")public void doBefore(JoinPoint joinPoint){//获取 requestServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();//获得url和ip...String classMethod = joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName();String url = request.getRequestURI().toString();String ip = request.getRemoteAddr();Object args [] = joinPoint.getArgs();RequestLog requestLog = new RequestLog(url,ip,classMethod,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) +'}';}}}

效果展示

在这里插入图片描述

  相关解决方案