Java Autowire在使用Spring Boot的电报应用程序中不工作
我使用java库制作电报机器人,并在应用程序中使用SpringBoot 在类TelegramBotPolling中,如果调用了方法onUpdateReceived,则字段busStationBtnsGenerator为空 调用onUpdateReceived方法时,如何正确地自动连接字段busStationBtnsGenerator,使其不为空 这是我的代码的简单示例:Java Autowire在使用Spring Boot的电报应用程序中不工作,java,spring-boot,telegram-bot,long-polling,Java,Spring Boot,Telegram Bot,Long Polling,我使用java库制作电报机器人,并在应用程序中使用SpringBoot 在类TelegramBotPolling中,如果调用了方法onUpdateReceived,则字段busStationBtnsGenerator为空 调用onUpdateReceived方法时,如何正确地自动连接字段busStationBtnsGenerator,使其不为空 这是我的代码的简单示例: @Component public class TelegramBotPolling extends TelegramLong
@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
{
.....
}
}
@组件
公共类TelegateBotPolling扩展了TelegateLongPollingBot{
@自动连线
母线站BTNSGenerator母线站BTNSGenerator;
静止的{
apiconteInitializer.init();
}
@施工后
公共作废登记薄(){
电报机器人API电报机器人API=新电报机器人API();
试一试{
telegramBotsApi.registerBot(新TelegramBotPolling());
}捕获(e){
错误(e);
}
}
@凌驾
已接收更新(更新){
//调用此方法时,“BussStationBTNSGenerator”字段为空。
}
}
@组成部分
公共类BusStationBtnsGeneratorImpl实现BusStationBtnsGenerator{
@自动连线
BusStationsParser-stationsParser;
@自动连线
UrlHelper UrlHelper;
@凌驾
public InlineKeyboardMarkup getKeyboardMarkupForBusStations()
抛出异常
{
......
}
私有列表getBusStationButtons()
抛出异常
{
.....
}
}
使用构造函数新建创建的类的实例不由Spring管理。在这种情况下,您应该使用这个关键字来引用它
@PostConstruct
public void registerBot(){
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
try {
telegramBotsApi.registerBot(this);
} catch (TelegramApiException e) {
logger.error(e);
}
使用构造函数new创建的类的实例不由Spring管理。在这种情况下,您应该使用这个关键字来引用它
@PostConstruct
public void registerBot(){
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
try {
telegramBotsApi.registerBot(this);
} catch (TelegramApiException e) {
logger.error(e);
}
请粘贴BusStationBtnsGenerator classis TelegramBotPolling类a spring bean?@Kick,我在问题中添加了**BusStationBtnsGenerator**类。@pvpkiran,no TelegramBotPolling不是spring bean类。这是我用来连接电报机器人的第三方库。电报机器人轮询类上没有任何注释,在上面添加组件请粘贴BusStationBtnsGenerator classis电报机器人轮询类a spring bean?@Kick,我在问题中添加了**BusStationBtnsGenerator**类。@pvpkiran,没有电报机器人轮询不是spring bean类。这是我用来连接电报机器人的第三方库。电报机器人类上没有任何注释,请在其上添加组件