【发布时间】:2017-09-15 13:52:31
【问题描述】:
我使用 java 库 TelegramBots 制作我的电报机器人,并在我的应用程序中使用 SpringBoot。
当在类 TelegramBotPolling 中调用方法 onUpdateReceived 时,字段 busStationBtnsGenerator 为空。
如何正确地自动装配字段 busStationBtnsGenerator 以便在调用 onUpdateReceived 方法时它不会为空?
这是我的代码的简短示例:
@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 类
-
TelegramBotPolling 类是 spring bean 吗?
-
@Kick,我在我的问题中添加了 **BusStationBtnsGenerator ** 类。
-
@pvpkiran,没有 TelegramBotPolling 不是 spring bean 类。它是我用来连接到我的电报机器人的第三方库。
-
TelegramBotPolling 类上没有任何注释,在上面添加组件
标签: java spring-boot telegram-bot long-polling