java实习_day13

中软国际实训第十三天——空属性赋值问题+aspect日志+shiro配置

空属性赋值问题

  1. 创建工具类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()]);
    }
}
  1. 完善service层中的方法
    @Override
    public News updateNew(Long id, News news) {

        News news1 = newRepository.findById(id).orElse(null);
        if (news1==null){
//            System.out.println("未获得更新对象");
        throw new NotFoundException("该新闻不存在");
        }
        BeanUtils.copyProperties(news,news1, MyBeanUtils.getNullPropertyNames(news));
        news1.setUpdateTime(new Date());
        return newRepository.save(news1);
    }
aspect日志
  1. 创建类LogAspect
@Aspect
@Component
public class LogAspect {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(* com.roger0123.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("Request :{}",requestLog);
        logger.info("--------------doBefore----------------");
    }

    @After("log()")
    public void doAfter(JoinPoint joinPoint){
        logger.info("--------------doAfter----------------");
    }

    @AfterReturning(returning = "result",pointcut = "log()")
    private 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 classMethod,Object[] args){
            this.url = url;
            this.ip = ip;
            this.classMethod = classMethod;
            this.args =args;
        }

        @Override
        public String toString() {
            return "RequestLog{" +
                    "url='" + url + '\'' +
                    ", ip='" + ip + '\'' +
                    ", classMethod='" + classMethod + '\'' +
                    ", args=" + Arrays.toString(args) +
                    '}';
        }
    }
}

结果展示

在这里插入图片描述

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页