空属性赋值
如何获得属性值为空的属性,需要用到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) +
'}';
}
}
}
效果展示: