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