0
我使用java庫TelegramBots來製作我的電報機器人並在我的應用程序中使用SpringBoot。Autowire不能在使用Spring Boot的電報應用程序中工作
當類TelegramBotPolling有被稱爲方法onUpdateReceived比場busStationBtnsGenerator爲空。
如何corectly爲了這將是不爲空時onUpdateReceived自動裝配領域busStationBtnsGenerator方法被調用?
這是我的代碼簡單的例子:有構造新創建的類的
@Component
public class TelegramBotPolling extends TelegramLongPollingBot {
@Autowired
BusStationBtnsGenerator busStationBtnsGenerator;
static {
ApiContextInitializer.init();
}
@PostConstruct
public void registerBot(){
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
try {
telegramBotsApi.registerBot(new TelegramBotPolling());
} catch (TelegramApiException e) {
logger.error(e);
}
}
@Override
public void onUpdateReceived(Update update) {
// When this method is called field "busStationBtnsGenerator" is null.
}
}
@Component
public class BusStationBtnsGeneratorImpl implements BusStationBtnsGenerator {
@Autowired
BusStationsParser stationsParser;
@Autowired
UrlHelper urlHelper;
@Override
public InlineKeyboardMarkup getKeyboardMarkupForBusStations()
throws Exception
{
......
}
private List<List<InlineKeyboardButton>> getBusStationButtons()
throws Exception
{
.....
}
}
請粘貼BusStationBtnsGenerator類 – Kick
是TelegramBotPolling類的spring bean嗎? – pvpkiran
@Kick,我在我的問題中添加了** BusStationBtnsGenerator **類。 – foxis