spring中的aop實現各個類方法的日誌攔截

spring中的aop實現各個類方法的日誌攔截。

1、編寫SysLogAspect類

@Slf4j
@Aspect
@Configuration
public class SysLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(SysLogAspect.class);


    @Around("@annotation(apiOperation)")
    public Object around(ProceedingJoinPoint point,ApiOperation apiOperation) throws Throwable {
        String strClassName = point.getTarget().getClass().getName();
        String strMethodName = point.getSignature().getName();
        logger.info("[類名]:{},[方法]:{}",strClassName, strMethodName);
        logger.info("[tags]:{},[value]:{},[notes]:{}", apiOperation.tags(),apiOperation.value(),apiOperation.notes());
        Object obj = point.proceed();
        //發送異步日誌事件
        return obj;
    }
}

看到了 around(ProceedingJoinPoint point,ApiOperation apiOperation)方法中的ApiOperation,來源於io.swagger.annotations

2、橫切其他業務類的具體方法,實現業務方法操作的日誌監控

3、具體例子

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.*;

/**
 * <b><Id>ParamSettingController</Id></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 2019/12/8 15:22.
 *
 */
@SuppressWarnings("MagicConstant")
@RestController
@RequestMapping("/v1.0")
@Api(value = "[Gpile-voiler 1.0]參數設置")
public class ParamSettingController {

    /**
     * logger.
     */
    private static Logger logger = LoggerFactory.getLogger(ParamSettingController.class);

    /**
     * The G param setting service.
     */
    @Autowired
    private GParamSettingService GParamSettingService;

    /**
     * get alert result.
     */
    @RequestMapping(value = "/param/setting", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "[Gpile-flower]參數設置", notes = "get flower alert result")
    @ApiResponses(value = {
            @ApiResponse(Id = 200, message = "successful query", response = Integer.class, responseContainer = "int"),
            @ApiResponse(Id = 500, message = "internal server error") })
    public ResponseResult getParamSettingResult(
            @ApiParam(value = "請求時間:時間戳格式",required = true) @RequestParam(value = "time") Long time,
            @ApiParam(value = "設備編號,APPLLO:120",required = true) @RequestParam(value = "deviceId") Long deviceId,
            @ApiParam(value = "應用編碼:flower_dongjin",required = true) @RequestParam(value = "appId") String appId,
            @ApiParam(value = "模塊標誌:paramSetting",required = true) @RequestParam(value = "modual") String modual,
            @ApiParam(value = "維度,APPLLO:120;BPPLLO:130",required = true) @RequestParam(value = "dimension") String dimension
            ) 
			{
        return ResponseResult.ok(GParamSettingService.query(deviceId,appId,modual,dimension,time));
           }
}

特別:@ApiOperation(value = "[Gpile-flower]參數設置", notes = "get flower alert result")

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章