最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中通過(guò)AOP整合日志文件的實(shí)現(xiàn)

 更新時(shí)間:2021年12月28日 10:07:32   作者:小公羊  
本文主要介紹了SpringBoot中通過(guò)AOP整合日志文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1.導(dǎo)入相關(guān)的依賴

<dependencies>
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions> <!-- 去掉springboot自帶的日志,不然會(huì)沖突 -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
 
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>
    <!-- log4j-api -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
    </dependency>
    <!-- log4j-slf4j-impl -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
    </dependency>
    <!-- slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
    <!-- slf4j-simple 解決沖突-->
    <!--<dependency>-->
        <!--<groupId>org.slf4j</groupId>-->
        <!--<artifactId>slf4j-simple</artifactId>-->
    <!--</dependency>-->
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
</dependencies>

2.log4j2 日志文件

<?xml version="1.0" encoding="UTF-8"?>
<!--日志級(jí)別以及優(yōu)先級(jí)排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration 后面的 status 用于設(shè)置 log4j2 自身內(nèi)部的信息輸出,可以不設(shè)置,當(dāng)設(shè)置成 trace 時(shí),可以看到 log4j2 內(nèi)部各種詳細(xì)輸出-->
<configuration status="INFO">
    <!--先定義所有的 appender-->
    <appenders>
        <!--輸出日志信息到控制臺(tái)-->
        <console name="Console" target="SYSTEM_OUT">
            <!--控制日志輸出的格式-->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </console>
    </appenders>
    <!--然后定義 logger,只有定義 logger 并引入的 appender,appender 才會(huì)生效-->
    <!--root:用于指定項(xiàng)目的根日志,如果沒(méi)有單獨(dú)指定 Logger,則會(huì)使用 root 作為默認(rèn)的日志輸出-->
    <loggers>
        <root level="info">
            <appender-ref ref="Console"/>
        </root>
    </loggers>
</configuration>

3.dao層的接口以及實(shí)現(xiàn)類

文件路徑: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("執(zhí)行了添加方法...");
    }
 
    @Override
    public void update() {
 
        System.out.println("執(zhí)行了修改方法...");
    }
 
    @Override
    public void delete() {
 
        System.out.println("執(zhí)行了刪除方法...");
    }
 
    @Override
    public void query() {
 
        System.out.println("執(zhí)行了查找方法...");
    }
}

4.Service層業(yè)務(wù)實(shí)現(xiàn)類

文件路徑: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.編寫業(yè)務(wù)類增強(qiáng)類,加入一個(gè)日志文件記錄

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){
 
        //這個(gè)RequestContextHolder是Springmvc提供來(lái)獲得請(qǐng)求的東西
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
 
        // 記錄下請(qǐng)求內(nèi)容
        log.info("################URL : " + request.getRequestURL().toString());//獲取請(qǐng)求的地址
        log.info("################HTTP_METHOD : " + request.getMethod());//獲取請(qǐng)求的方法
        log.info("################IP : " + request.getRemoteAddr());
        log.info("################THE ARGS OF THE CONTROLLER : " + Arrays.toString(joinPoint.getArgs()));
 
        /**
         * getSignature().getDeclaringTypeName()   是獲取包+類名的
         * joinPoint.getSignature.getName()  獲取了方法名
         * joinPoint.getTarget() 返回的是需要加強(qiáng)的目標(biāo)類的對(duì)象
         * joinPoint.getThis())  返回的是經(jīng)過(guò)加強(qiáng)后的代理類的對(duì)象
         */
        log.info("################CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        log.info("################TARGET: " + joinPoint.getTarget());
        log.info("################THIS: " + joinPoint.getThis());
        System.out.println("方法執(zhí)行之前...");
    }
 
    @After("webLog()")
    public void after(){
        System.out.println("方法執(zhí)行之后...");
    }
}

7.運(yùn)行測(cè)試,查看結(jié)果

啟動(dòng)主啟動(dòng)器類,在瀏覽器中輸入http://localhost:8080/test,查看控制臺(tái)輸出信息

到此這篇關(guān)于SpringBoot中通過(guò)AOP整合日志文件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot AOP日志文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringCloud之微服務(wù)容錯(cuò)的實(shí)現(xiàn)

    SpringCloud之微服務(wù)容錯(cuò)的實(shí)現(xiàn)

    這篇文章主要介紹了SpringCloud之微服務(wù)容錯(cuò)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • SpringBoot2 整合FreeMarker實(shí)現(xiàn)頁(yè)面靜態(tài)化示例詳解

    SpringBoot2 整合FreeMarker實(shí)現(xiàn)頁(yè)面靜態(tài)化示例詳解

    這篇文章主要介紹了SpringBoot2 整合FreeMarker實(shí)現(xiàn)頁(yè)面靜態(tài)化示例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Spring?Cloud?Eureka服務(wù)注冊(cè)中心入門流程分析

    Spring?Cloud?Eureka服務(wù)注冊(cè)中心入門流程分析

    這篇文章主要介紹了Spring?Cloud?Eureka服務(wù)注冊(cè)中心入門流程分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Mybatis-Plus注入SQL原理分析

    Mybatis-Plus注入SQL原理分析

    本文主要介紹了Mybatis-Plus注入SQL原理分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Java使用JDBC實(shí)現(xiàn)Oracle用戶認(rèn)證的方法詳解

    Java使用JDBC實(shí)現(xiàn)Oracle用戶認(rèn)證的方法詳解

    這篇文章主要介紹了Java使用JDBC實(shí)現(xiàn)Oracle用戶認(rèn)證的方法,結(jié)合實(shí)例形式分析了java使用jdbc實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接、建表、添加用戶、用戶認(rèn)證等操作流程與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-08-08
  • 深入理解Java SpringCloud Ribbon 負(fù)載均衡

    深入理解Java SpringCloud Ribbon 負(fù)載均衡

    Ribbon是一個(gè)客戶端負(fù)載均衡器,它提供了對(duì)HTTP和TCP客戶端的行為的大量控制。這篇文章主要介紹了SpringCloud Ribbon 負(fù)載均衡的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2021-09-09
  • Spring MVC的優(yōu)點(diǎn)與核心接口_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Spring MVC的優(yōu)點(diǎn)與核心接口_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了Spring MVC的優(yōu)點(diǎn)與核心接口,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • Java參數(shù)校驗(yàn)Validator與@AssertTrue深度解析

    Java參數(shù)校驗(yàn)Validator與@AssertTrue深度解析

    本文詳細(xì)介紹了Java的Validator框架及其@AssertTrue注解的使用,包括環(huán)境準(zhǔn)備、基礎(chǔ)注解介紹、實(shí)戰(zhàn)示例、@AssertTrue的深入解析、高級(jí)特性和最佳實(shí)踐建議,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • Java 獲取當(dāng)前時(shí)間及實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能【推薦】

    Java 獲取當(dāng)前時(shí)間及實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能【推薦】

    這篇文章主要介紹了Java 獲取當(dāng)前時(shí)間及實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能 ,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Java實(shí)現(xiàn)軟件運(yùn)行時(shí)啟動(dòng)信息窗口的方法

    Java實(shí)現(xiàn)軟件運(yùn)行時(shí)啟動(dòng)信息窗口的方法

    這篇文章主要介紹了Java實(shí)現(xiàn)軟件運(yùn)行時(shí)啟動(dòng)信息窗口的方法,比較實(shí)用的功能,需要的朋友可以參考下
    2014-08-08

最新評(píng)論

凯里市| 吴江市| 盐津县| 汝州市| 茂名市| 沾益县| 永济市| 大名县| 交城县| 佛教| 深水埗区| 大同市| 墨竹工卡县| 龙陵县| 江安县| 宁海县| 河曲县| 甘孜县| 安庆市| 西丰县| 建水县| 张家界市| 邳州市| 杂多县| 化州市| 津南区| 新兴县| 涿鹿县| 清新县| 右玉县| 唐河县| 六枝特区| 鄢陵县| 白银市| 南京市| 邵阳市| 东乌珠穆沁旗| 馆陶县| 怀远县| 喜德县| 峨边|