Spring Boot Autowired存储库为空

我正在尝试创建一个显示货币汇率的电报机器人。但是,我遇到了@Autowired JPA存储库的问题。 在Currency.java中,我可以访问rateRepository并将信息放入数据库中。但是,在Bot.java类中,我无法访问@Autowired rateRepository。因为它为空。

enter image description here

package com.example.demo;

import com.example.demo.entity.Rate;
import com.example.demo.repository.RateRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.util.ArrayList;
import java.util.List;

@Component
@EnableJpaRepositories
public class Bot extends TelegramLongPollingBot {

@Autowired
private RateRepository rateRepository;   //NULL

public static SendMessage sendButton(long id){
    SendMessage message = new SendMessage();
    message.setChatId(id);
    InlineKeyboardMarkup markupInline = new InlineKeyboardMarkup();
    List<List<InlineKeyboardButton>> rowsInline = new ArrayList<>();
    List<InlineKeyboardButton> rowInline = new ArrayList<>();
    rowInline.add(new InlineKeyboardButton().setText("Get info").setCallbackData("get_info"));
    rowsInline.add(rowInline);
    markupInline.setKeyboard(rowsInline);
    message.setReplyMarkup(markupInline);
    message.setText("Press the button to get info");
    return message;
}

public void onUpdateReceived(Update update) {

    if(update.hasMessage() && update.getMessage().hasText()) {
        if(update.getMessage().getText().equals("/start")){
            try {
                execute(sendButton(update.getMessage().getChatId()));
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    } else if(update.hasCallbackQuery()){
        if(update.getCallbackQuery().getData().equals("get_info")){
            Rate rate = rateRepository.getOne(1L);
            String result = "EUR : " + rate.getEur() + "\n" +
                    "USD : " + rate.getUsd() + "\n" +
                    "RUB : " + rate.getRub() + "\n";
            EditMessageText new_message = new EditMessageText()
                    .setChatId(update.getCallbackQuery().getMessage().getChatId())
                    .setMessageId(update.getCallbackQuery().getMessage().getMessageId())
                    .setText(result);
            try {
                execute(new_message);
                execute(sendButton(update.getCallbackQuery().getMessage().getChatId()));
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

}

public String getBotUsername() {
    return "currency_kzt_bot";
}

public String getBotToken() {
    return "My token";
}

}

Currency.java

package com.example.demo;

import com.example.demo.entity.Rate;
import com.example.demo.repository.RateRepository;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

@Component
@EnableScheduling
public class Currency {

@Autowired
private RateRepository rateRepository;   // NOT NULL

private static final String  API_KEY = "my key";
private static final String REQUEST = "https://currate.ru/api/?get=rates&pairs=USDKZT,EURKZT,RUBKZT&key=";


@Scheduled(fixedRate = 600000)
public void getRates(){
    try {
        URL url =new URL(REQUEST + API_KEY);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = input.readLine()) != null) {
            response.append(inputLine);
        }
        input.close();
        HashMap<String, Double> data = parse(response.toString());
        Rate rate = new Rate(data.get("EUR"), data.get("USD"), data.get("RUB"));
        rateRepository.save(rate);

    } catch (IOException e) {
        e.printStackTrace();
    }

}

public HashMap<String, Double> parse(String json){

    HashMap<String, Double> data = new HashMap<>();
    JSONObject object = new JSONObject(json);
    Double usd = object.getJSONObject("data").getDouble("USDKZT");
    Double eur = object.getJSONObject("data").getDouble("EURKZT");
    Double rub = object.getJSONObject("data").getDouble("RUBKZT");
    data.put("USD", usd);
    data.put("EUR", eur);
    data.put("RUB", rub);

    return data;

}

}

RateRepository.java

package org.example.demo.repository;

import org.example.demo.entity.Rate;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RateRepository extends JpaRepository<Rate, Long> {
    Rate findTopByOrderByIdDesc();
}

DemoApplication.java

package org.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

@SpringBootApplication
public class Main {

    public static void main(String[] args) {
        ApiContextInitializer.init();
        TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
        SpringApplication.run(Main.class, args);
        try {
            telegramBotsApi.registerBot(new Bot());
        } catch (TelegramApiException e){
            e.printStackTrace();
        }

    }

}

Rate.java

package org.example.demo.entity;

import javax.persistence.*;

@Entity
@Table(name = "rate")
public class Rate {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "eur")
    private double eur;

    @Column(name = "usd")
    private double usd;

    @Column(name = "rub")
    private double rub;

    public Rate(){

    }

    public Rate(double eur, double usd, double rub) {
        this.eur = eur;
        this.usd = usd;
        this.rub = rub;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public double getEur() {
        return eur;
    }

    public void setEur(double eur) {
        this.eur = eur;
    }

    public double getUsd() {
        return usd;
    }

    public void setUsd(double usd) {
        this.usd = usd;
    }

    public double getRub() {
        return rub;
    }

    public void setRub(double rub) {
        this.rub = rub;
    }
}