[个人笔记]基于SpringBoot的新闻管理系统 空属性赋值问题+aspect日志功能

空属性赋值

如何获得属性值为空的属性,需要用到BeanUtils这个类里的方法
需要写一个工具类 所以在Util内新建一个MyBeanUtils

public class NewsRealm extends AuthorizingRealm {
    public void setName(String name){setName("newsRealm");}

    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取认证的用户数据
        User user = (User)principalCollection.getPrimaryPrincipal();
        //构造认证数据
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<Role> roles = user.getRoles();
        for (Role role:roles){
            //获得角色信息
            info.addRole(role.getName());
            for (Permission permission:role.getPermissions()){
                //添加权限信息
                info.addStringPermission(permission.getCode());
            }
        }
        return info;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken)authenticationToken;
        String username = upToken.getUsername();
        String password = new String(upToken.getPassword());
        User user = userService.checkUsers(username, password);
        if (user!=null){
            return  new SimpleAuthenticationInfo(user,user.getPassword(),this.getName());
        }
        return null;
    }

然后在NewServiceImpl里将update这个方法进行修改

  @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);

    }

这样一来所找到的空属性就会被抛弃忽略掉,数据库中null的地方将会保住,而不会消失;比如说更新时间和创建时间,当更新时间更新了,创建时间就不会被更新时间所覆盖或者创建时间直接丢失
页面显示效果:
在这里插入图片描述
数据库显示:
在这里插入图片描述## aspect日志功能
运用切面
我们每一个要单独拿出来的功能要被反复使用到的,为了减少每次使用都要进行new操作,反复去写。所以用到切面,当某个地方要用,我们就把这个切面切进去。

//切面
@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.getRequestURI().toString();
        String ip = request.getLocalAddr();
        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(){
        logger.info("------------doBefore-----------");
    }

    @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 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) +
                    '}';
        }
    }


}

效果展示:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值