SpringBoot中通过AOP整合日志文件的实现

1.导入相关的依赖


 
    
        org.springframework.boot
        spring-boot-starter
         
            
                org.springframework.boot
                spring-boot-starter-logging
            
        
    
 
    
        org.apache.logging.log4j
        log4j-core
    
    
    
        org.apache.logging.log4j
        log4j-api
    
    
    
        org.apache.logging.log4j
        log4j-slf4j-impl
    
    
    
        org.slf4j
        slf4j-api
    
    
    
        
        
    
 
    
        org.springframework.boot
        spring-boot-starter-aop
    
 
    
        org.springframework.boot
        spring-boot-starter-web
    
 
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        log4j
        log4j
        1.2.12
    

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.add();
    }
    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,查看控制台输出信息

SpringBoot中通过AOP整合日志文件的实现_第1张图片

到此这篇关于SpringBoot中通过AOP整合日志文件的实现的文章就介绍到这了,更多相关SpringBoot AOP日志文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的