1、解决空属性赋值问题
①、新建一个MyBeanUtils工具类封装空属性赋值方法
public classMyBeanUtils {public staticString[] getNullPropertyNames(Object source){
BeanWrapper beanWrapper= newBeanWrapperImpl(source);
PropertyDescriptor[] pds=beanWrapper.getPropertyDescriptors();
List nullPropertyNames = new ArrayList<>();for(PropertyDescriptor pd:pds){
String propertyName=pd.getName();if(beanWrapper.getPropertyValue(propertyName)==null){
nullPropertyNames.add(propertyName);
}
}return nullPropertyNames.toArray(newString[nullPropertyNames.size()]);
}
}
②、在NewsServiceImpl类中的updateNew方法中进行修改,替换原BeanUtils.copyProperties(news, news1)为下面的语句
BeanUtils.copyProperties(news, news1, MyBeanUtils.getNullPropertyNames(news));
并在new1为空时抛出new NotFoundException("该新闻不存在");异常
2、aspect日志
运用到面向切面编程(AOP)的思想。
在AOP的逻辑内,先走@Around注解的方法。然后是@Before注解的方法,然后这两个都通过了,走核心代码,核心代码走完,无论核心有没有返回值,都会走@After方法。然后如果程序无异常,正常返回就走@AfterReturn,有异常就走@AfterThrowing。
①、新建LogAspect类,类注解为@Aspect和@Component
②、在类中编写如下代码
private final Logger logger = LoggerFa