2017-09-15 292 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); 
} 
相关问题