第三方库方法调用不触发调用尊重AOP方法

时间:2017-09-19 08:45:53

标签: java spring-boot aspectj telegram-bot

我在我的应用程序中使用java库TelegramBots,Spring Boot和AspectJ创建了我的电报机器人。

我有下一个课程,用类TelegramLongPollingBot扩展第三方库类TelegramBotPolling

 @Component
 public class TelegramBotPolling extends TelegramLongPollingBot {
    static {
       ApiContextInitializer.init();
    }

    @PostConstruct
    public void registerBot(){
       TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
       try {
          telegramBotsApi.registerBot(this);
       } catch (TelegramApiException e) {
          logger.error(e);
       }
    }

    @Override
    public void onUpdateReceived(Update update) {   
       ...
    }   
}

这个库类有我在类onUpdateReceived中实现的抽象方法TelegramBotPolling

我希望在调用方法onUpdateReceived时跟踪并使用Spring AOP记录此方法:

@Aspect
@Component
public class TelegramBotPollingLogger {

   @Around("execution(* ru.cheb.intercity.bus.telegrambot.TelegramBotPolling.onUpdateReceived(..))")
   public Object onUpdateReceivedMethodLogger(ProceedingJoinPoint joinPoint)
   {
        Object returnVal = commonLogHandler.handle(joinPoint);
        return returnVal;
   }
}

但是当调用onUpdateReceivedMethodLogger时,AOP方法onUpdateReceived不会调用。我想我有一个问题,因为onUpdateReceived与第三方图书馆有关。因为我自己的类方法适用于AOP。

当调用方法onUpdateReceivedMethodLogger时,如何调用AOP方法onUpdateReceived

============================================

尝试在其他类中注入TelegramBotPolling类:

  @Component
 public class TelegramBotPolling extends TelegramLongPollingBot implements  TelegramBotUpdater{
    static {
       ApiContextInitializer.init();
    }

    @Override
    public String getBotToken() {...}

    @Override  
    public String getBotUsername() {...}

    @Override
    public void onUpdateReceived(Update update) {   
       ...
    }   
}

我为TelegramBotUpdater

定义了接口TelegramBotPolling
  public interface TelegramBotUpdater {

     String getBotToken();

     String getBotUsername();

     void onUpdateReceived(Update update);

 }

注入此接口的其他类TelegramBotUpdater

@Component
public class BotRegistrator {



   @Autowired
   TelegramBotUpdater telegramBotUpdater;

   public void register(){
      TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
      try {
          telegramBotsApi.registerBot((TelegramBotPolling)telegramBotUpdater);
      } catch (TelegramApiException e) {
          logger.error(e);
      }
   }

}

启动spring boot应用程序的Application类会注入其他类:

@SpringBootApplication
public class Application {

     @Autowired
     BotRegistrator botRegistrator;

    public static void main(String[] args) throws Throwable {

        SpringApplication.run(Application.class, args);

        botRegistrator.register();
   }
}

调用botRegistrator.register();时,我会收到NPE异常。

0 个答案:

没有答案