SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数
SpringBoot使用AOP统一处理请求日志
这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理。首先在pom.xml中引入我们需要的aop的jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
然后我们新建一个Aspect去对我们的所有Controller进行一个日志拦截。在SPringBoot中我们使用AOP也是很简单的,只需要在类上加上一个@Aspect的注解就好了,然后通过@Component注解到我们的Spring容器中去。
我们新建一个包com.majiaxueyuan.log专门用来放我们的日志记录,代码如下:
@Aspect
@Component
@Slf4j
public class MjxyLogAspect {
//第一个 * 号表示任意返回类型,第二个 * 号表示Person的所有方法
@Pointcut("execution(public * com.majiaxueyuan.controller..*.*(..))")
public void webLog() {
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
log.info("URL : " + request.getRequestURL().toString());
log.info("HTTP_METHOD : " + request.getMethod());
log.info("IP : " + request.getRemoteAddr());
Enumeration<String> enu = request.getParameterNames();
while (enu.hasMoreElements()) {
String name = (String) enu.nextElement();
log.info("name:{},value:{}", name, request.getParameter(name));
}
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
log.info("RESPONSE : " + ret);
}
}
然后我们通过浏览器随便请求一次,可以看到控制台输出,这里我们就可以在实际生产环境中存储日志。
SpringBoot定时任务@Scheduled
在service中新建一个类ScheduledTest,代码如下:
@Component
public class ScheduledTest {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 2000)
public void scheduled() {
System.out.println("码家学院提示你==》现在时间:" + dateFormat.format(new Date()));
}
}
另外我们需要在主函数启动类上开启定时器
@SpringBootApplication
@EnableScheduling
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
SpringBoot异步调用Async
这个和定时器差不多,启动加上@EnableAsync ,只需要在我们需要异步的方法上面加上@Async注解
异步就是在主线程中部分代码执行另一个线程,而另一个线程不影响主线程的执行。
AsyncTest.java
@Component
public class AsyncTest{
@Async
public void asyncOut(){
sout(Thread.currentThread().geetId());
}
}
启动类中
@SpringBootApplication
@EnableAsync
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
@RequestMapping("/login")
public String login(){
sout(Thread.currentThread().getId());
asyncTest.asyncOut();
return "loginPage"
}
运行的结果发现两个id是不同的,说明成功了。
自定义参数
在配置文件中我们可以去在配置文件中自定义一些参数,比如:
name=majiaxueyuan
我们在代码中获取到我们的这个配置文件
@Value("${name}")
private String name;
@ResponseBody
@RequestMapping("/getValue")
public String getValue() {
return name;
}
SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数的更多相关文章
- Spring Boot 表单验证、AOP统一处理请求日志、单元测试
一.使用@Valid表单验证 于实体类中添加@Min等注解 @Entity public class Girl { @Id @GeneratedValue private Integer id; pr ...
- Springboot中AOP统一处理请求日志
完善上面的代码: 现在把输出信息由先前的system.out.println()方式改为由日志输出(日志输出的信息更全面) 现在在日志中输出http请求的内容 在日志中获取方法返回的内容
- SpringBoot学习笔记
SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...
- springboot 学习笔记(二)
springboot 学习笔记(二) 快速创建一个springboot工程,并引入所需要的依赖 1.利用Spring initializr 来创建一个springboot项目,登陆http://sta ...
- SpringBoot学习笔记(7):Druid使用心得
SpringBoot学习笔记(7):Druid使用心得 快速开始 添加依赖 <dependency> <groupId>com.alibaba</groupId> ...
- SpringBoot学习笔记(8):事物处理
SpringBoot学习笔记(8):事物处理 快速入门 在传统的JDBC事务代码开发过程中,业务代码只有一部分,大部分都是与JDBC有关的功能代码,比如数据库的获取与关闭以及事务的提交与回滚.大量的t ...
- springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源
本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...
- Springboot学习笔记(六)-配置化注入
前言 前面写过一个Springboot学习笔记(一)-线程池的简化及使用,发现有个缺陷,打个比方,我这个线程池写在一个公用服务中,各项参数都定死了,现在有两个服务要调用它,一个服务的线程数通常很多,而 ...
- SpringBoot学习笔记(14):使用SpringBootAdmin管理监控你的应用
SpringBoot学习笔记(14):使用SpringBootAdmin管理监控你的应用 Spring Boot Admin是一个管理和监控Spring Boot应用程序的应用程序.本文参考文档: 官 ...
随机推荐
- python生成当前时间的时间戳
有时会用到时间戳,查了相关资料,在这里记一下 主要有两种方案: import datetime time_stamp = '{0:%Y-%m-%d-%H-%M}'.format(datetime.da ...
- sails中创建和使用services
从sails官方在线文档查知 // EmailService.js - in api/services module.exports = { sendInviteEmail: function(opt ...
- uoj140 【UER #4】被粉碎的数字
题目 看起来就像是数位\(\rm dp\) 不妨从竖式乘法的角度来考虑这个问题 为了方便处理进位,我们得从低位向高位填数 设\(dp[i][0/1][j][p][t]\)表示填到了第\(i\)位,卡不 ...
- sort()和优先队列的总结
一.关于sort函数 sort()排序函数默认是从小到大, a={5,3,2,1,6 }; sort(a,a+n); //输出是1 2 3 5 6 这里如果要从到小排序,则有两种方式可以满足 (1) ...
- 最短路(模板Dtra
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const i ...
- iOS开发系列-NSFileManager
NSFileManager NSFileManager类主要对文件和目录的操作(删除.修改.移动.复制等等).
- PHP算法之盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- Kubernetes的包管理工具Helm的安装和使用
1.源码安装 [root@master ~]# wget https://storage.googleapis.com/kubernetes-helm/helm-v2.14.0-linux-amd64 ...
- NOIP2016 解题报告
D1T1 玩具谜题 xjb模拟即可 #include<bits/stdc++.h> #define N (100000+5) using namespace std; inline int ...
- 使用Windbg调试系统弹出的内存不可读错误
步骤: 1. 使用Windbg挂钩到崩溃的进程上面 2. 使用~*k列出所有线程 3. 搜索UnhandledExceptionFilter所在的线程 4. 使用~ns切换到上面崩溃所在的线程,n为线 ...