在Spring Boot应用程序中,对于每个方法调用,Aspect类中的方法都会被调用两次

时间:2018-10-23 07:56:40

标签: spring spring-aop

@Aspect
public class ControllerLogger {

    @Autowired
    InfobloxLogsMapper infobloxLogsMapper;

    Logger log = LoggerFactory.getLogger(ControllerLogger.class);
    LogResquestAndResponse logsMapper = new LogResquestAndResponse();

    private final RequestInformationProvider infoProvider;

    public ControllerLogger() {
        infoProvider = new RequestInformationProvider();
    }

    // for unit testing only
    ControllerLogger(Logger log, RequestInformationProvider infoProvider) {
                this.infoProvider = infoProvider;
                this.log = log;
            }

    @Before("execution(* com.tr.dco.fp.ipam.controller..*.*(..))")
    public void logBefore(JoinPoint joinPoint){
        List<Object> paramList =Arrays.asList(joinPoint.getArgs());

        LogResquestAndResponse logs = infobloxLogsMapper.mapRequestResponsetoObject(logsMapper,paramList, infoProvider ,null, null );
        log.info("When=Before, User={}, Requester={}, Method={}, ParamList={}, Source= {}, Reference={} ", infoProvider.getCurrentUsername(),
                joinPoint.getSignature().toShortString(), paramList, infoProvider.getSourceSystem(), infoProvider.getReference());
    }

    @AfterReturning(pointcut = "execution(* com.tr.dco.fp.ipam.controller..*.*(..))", returning = "retVal")
    public void logAfter(JoinPoint joinPoint, ResponseEntity<?> retVal) {   
        List<Object> paramList =Arrays.asList(joinPoint.getArgs());
        LogResquestAndResponse logs = infobloxLogsMapper.mapRequestResponsetoObject(logsMapper,paramList, infoProvider ,retVal, null);
        List<LogResquestAndResponse> jsonList = new ArrayList<LogResquestAndResponse>();
        jsonList.add(logs);
        try{
        ObjectMapper mapper = new ObjectMapper();
        String jsonlogs = mapper.writeValueAsString(jsonList);
        saveToArongo(jsonlogs);
    } catch (JsonProcessingException e) {
        log.warn(e.getMessage());
    }}

    @AfterThrowing(pointcut = "execution(* com.tr.dco.fp.ipam.controller..*.*(..))", throwing = "ex")
    public void logError(JoinPoint joinPoint, Exception ex) {
        List<Object> paramList =Arrays.asList(joinPoint.getArgs());
        LogResquestAndResponse logs = infobloxLogsMapper.mapRequestResponsetoObject(logsMapper,paramList, infoProvider ,null, ex );
        List<LogResquestAndResponse> jsonList = new ArrayList<LogResquestAndResponse>();
        jsonList.add(logs);
            ObjectMapper mapper = new ObjectMapper();           
            String jsonlogs;
            try {
                jsonlogs = mapper.writeValueAsString(jsonList);
                saveToArongo(jsonlogs);
            } catch (JsonProcessingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }


    public void saveToArongo(String jsonArray) {
        //saving in db
}

创建API调用控制器类时。首先在执行方法之前,然后是实际方法,然后是throwing方法。之后,再次调用Before方法,然后调用After返回方法。 无法指出为什么它被调用两次。需要帮助。

1 个答案:

答案 0 :(得分:0)

我解决了问题。 同一包中包含一个异常处理程序类,该异常类在引发异常的情况下被调用,并再次调用我的before方法。