edm项目5.3总结
程序员文章站
2024-01-19 14:50:46
...
一、lombok的使用
maven引入
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
场景一、使用log打印日志
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class MailProducerApplicationTests {
@Test
public void contextLoads() {
log.info("hello world!");
}
}
场景二、DTO上使用
使用@Data,不需要再手动生成getter setter toString hsahCode等方法
import javax.validation.constraints.NotEmpty;
@Data
public class MailSendDTO {
@NotEmpty
private String sendTo;
@NotEmpty
private String sendMail;
@NotEmpty
private String sendContent;
@NotEmpty
private Long sendPriority;
}
其他场景
单独使用@Getter @Setter @ToString等更精细的注解;@Cleanup可以用于关闭流;
二、应用AOP为每个接口织入日志代码
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
日志代码
@Aspect
@Component
@Slf4j
public class WebLogAspect {
@Pointcut("execution(public * com.hgc.*.api.*.*(..))")
public void logPointCut() {
}
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
//打印接口信息
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.info("---------------request start---------------");
log.info("url={}",request.getRequestURL());
log.info("method={}",request.getMethod());
log.info("class_method={}", point.getSignature().getDeclaringTypeName() + "." + point.getSignature().getName());
log.info("args={}", point.getArgs());
long beginTime = System.currentTimeMillis();
Object result = point.proceed();
long time = System.currentTimeMillis() - beginTime;
log.info("response={}", FastJsonConvertUtil.convertObjectToJSON(result));
log.info("cost time={}",time);
log.info("---------------request end---------------");
return result;
}
}
上一篇: 读取文本文件
下一篇: 读取文本文件,将其边问pandas表格