我在我的应用程序中使用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异常。