java 单机接口限流处理方案
239
2022-09-06
SpringBoot中通过AOP整合日志文件的实现
目录1.导入相关的依赖2.log4j2 日志文件3.dao层的接口以及实现类4.Service层业务实现类5.Controllerhttp://层接口控制类6.编写业务类增强类,加入一个日志文件记录7.运行测试,查看结果
1.导入相关的依赖
http://
2.log4j2 日志文件
3.dao层的接口以及实现类
文件路径:com.dzj.dao.Userdao.java
package com.dzj.dao;
public interface Userdao {
void add();
void update();
void delete();
void query();
}
文件路径:com.dzj.dao.UserDaoImpl.java
package com.dzj.dao;
import org.springframework.stereotype.Service;
@Service
public class UserDaoImpl implements Userdao {
@Override
public void add() {
System.out.println("执行了添加方法...");
}
@Override
public void update() {
System.out.println("执行了修改方法...");
}
@Override
public void delete() {
System.out.println("执行了删除方法...");
}
@Override
public void query() {
System.out.println("执行了查找方法...");
}
}
4.Service层业务实现类
文件路径:com.dzj.service.UserService.java
package com.dzj.service;
import com.dzj.dao.UserDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserDaoImpl userDao;
public void add(){
userDao.adyPZyDd();
}
public void update(){
userDao.update();
}
public void delete(){
userDao.delete();
}
public void query(){
userDao.query();
}
}
5.Controller层接口控制类
文件路径:com.dzj.controller.UserController.java
package com.dzj.controller;
import com.dzj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/test")
public String userTest(){
userService.add();
return "chenggongle...";
}
}
6.编写业务类增强类,加入一个日志文件记录
package com.dzj.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
@Component
@Aspect
public class AdviceMethod {
private static final Logger log = LoggerFactory.getLogger(AdviceMethod.class);
@Pointcut("execution(public * com.dzj.service.UserService.*(..))")
public void webLog(){}
@Before("webLog()")
public void before(JoinPoint joinPoint){
//这个RequestContextHolder是Springmvc提供来获得请求的东西
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
// 记录下请求内容
log.info("################URL : " + request.getRequestURL().toString());//获取请求的地址
log.info("################HTTP_METHOD : " + request.getMethod());//获取请求的方法
log.info("################IP : " + request.getRemoteAddr());
log.info("################THE ARGS OF THE CONTROLLER : " + Arrays.toString(joinPoint.getArgs()));
/**
* getSignature().getDeclaringTypeName() 是获取包+类名的
* joinPoint.getSignature.getName() 获取了方法名
* joinPoint.getTarget() 返回的是需要加强的目标类的对象
* joinPoint.getThis()) 返回的是经过加强后的代理类的对象
*/
log.info("################CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
log.info("################TARGET: " + joinPoint.getTarget());
log.info("################THIS: " + joinPoint.getThis());
System.out.println("方法执行之前...");
}
@After("webLog()")
public void after(){
System.out.println("方法执行之后...");
}
}
7.运行测试,查看结果
启动主启动器类,在浏览器中输入http://localhost:8080/test,查看控制台输出信息
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~