spring boot空屬性賦值問題與aspect日志實(shí)現(xiàn)方法
空屬性賦值問題
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()]);
}
}
在NewServiceImpl中對updateNew方法進(jìn)行修改
@Override
public News updateNew(Long id, News news) {
News news1=newRepository.findById(id).orElse(null);
if(news1==null){
// System.out.println("未獲得更新對象");
throw new NotFoundException("該新聞不存在");
}
//更新后傳入的news復(fù)制給news1,查找更新數(shù)據(jù)news中空值屬性,忽略不復(fù)制給news1
BeanUtils.copyProperties(news,news1, MyBeanUtils.getNullPropertyNames(news));
news1.setUpdateTime(new Date());
return newRepository.save(news1);
}
日志打印
新建一個(gè)LogAspect類
@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.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("_____________________doBefore_______________________");
}
@After("log()")
public void doAfter(){
logger.info("_____________________doAfter_______________________");
}
@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 className, Object[] args) {
this.url = url;
this.ip = ip;
this.classMethod = className;
this.args = args;
}
@Override
public String toString() {
return "Requestlog{" +
"url='" + url + '\'' +
", ip='" + ip + '\'' +
", classMethod='" + classMethod + '\'' +
", args=" + Arrays.toString(args) +
'}';
}
}
}
效果展示

總結(jié)
到此這篇關(guān)于spring boot空屬性賦值問題與aspect日志實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)spring boot空屬性賦值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot深入分析運(yùn)行原理與功能實(shí)現(xiàn)
我們發(fā)現(xiàn)springBoot程序開發(fā)比spring程序編寫起來容易的多。配置簡潔,依賴關(guān)系簡單,啟動(dòng)運(yùn)行容易。那么結(jié)下了我們我們就要思考一下入門程序中的這些功能是怎么實(shí)現(xiàn)的2022-09-09
springboot+springsecurity如何實(shí)現(xiàn)動(dòng)態(tài)url細(xì)粒度權(quán)限認(rèn)證
這篇文章主要介紹了springboot+springsecurity如何實(shí)現(xiàn)動(dòng)態(tài)url細(xì)粒度權(quán)限認(rèn)證的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
java web開發(fā)中大量數(shù)據(jù)導(dǎo)出Excel超時(shí)(504)問題解決
開發(fā)測試時(shí)候?qū)霐?shù)據(jù)遇到大數(shù)據(jù)導(dǎo)入的問題,整理了下,需要的朋友可以參考下2017-04-04
JavaWeb Struts文件上傳功能實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了JavaWeb Struts文件上傳功能實(shí)現(xiàn)過程,思路清晰,供大家參考,感興趣的小伙伴們可以參考一下2016-06-06
MyBatis-Plus 使用枚舉自動(dòng)關(guān)聯(lián)注入
本文主要介紹了MyBatis-Plus 使用枚舉自動(dòng)關(guān)聯(lián)注入,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
解決mybatis-plus使用jdk8的LocalDateTime 查詢時(shí)報(bào)錯(cuò)的方法
這篇文章主要介紹了解決mybatis-plus使用jdk8的LocalDateTime 查詢時(shí)報(bào)錯(cuò)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java實(shí)現(xiàn)AC自動(dòng)機(jī)全文檢索示例
本篇文章主要介紹了Java實(shí)現(xiàn)AC自動(dòng)機(jī)全文檢索示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
Java8默認(rèn)方法Default Methods原理及實(shí)例詳解
這篇文章主要介紹了Java8默認(rèn)方法Default Methods原理及實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
關(guān)于java.lang.NumberFormatException: null的問題及解決
這篇文章主要介紹了關(guān)于java.lang.NumberFormatException: null的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

