2017-09-15 289 views
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 
    { 
     ..... 
    } 

} 
+1

請粘貼BusStationBtnsGenerator類 – Kick

+2

是TelegramBotPolling類的spring bean嗎? – pvpkiran

+0

@Kick,我在我的問題中添加了** BusStationBtnsGenerator **類。 – foxis

回答

2

實例不是由Spring管理。爲了引用它,你應該在這種情況下這個關鍵字。

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