Добрый день.
Разбираюсь с примером по работе со spring и telegram.
Бот должен создавать автоматически таблицу (usersDataTable) в mysql (если такой еще нет) и записывать пользователя, если там его нет.
Делаю по примеру с youtube ссылка на репозиторий (
https://github.com/DmitrijsFinaskins/simple-telegr...).
Main:
package ru.trakivanBot.trakivanBot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class TrakivanBotApplication {
public static void main(String[] args) {
SpringApplication.run(TrakivanBotApplication.class, args);
}
}
userRepository
package ru.trakivanBot.trakivanBot.model;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
}
user
package ru.trakivanBot.trakivanBot.model;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import org.springframework.data.annotation.Id;
import java.sql.Timestamp;
@Entity(name = "usersDataTable")
//@Table(name = "usersDataTable")
public class User {
@Id
private Long chatId;
private String firstName;
private String lastName;
private String userName;
}
Telegrambot
package ru.trakivanBot.trakivanBot.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.commands.BotCommand;
import org.telegram.telegrambots.meta.api.objects.commands.scope.BotCommandScopeDefault;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import ru.trakivanBot.trakivanBot.config.BotConfig;
import ru.trakivanBot.trakivanBot.model.User;
import ru.trakivanBot.trakivanBot.model.UserRepository;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Component
public class TelegramBot extends TelegramLongPollingBot {
@Autowired
private UserRepository userRepository;
final BotConfig config;
static final String HELP_TEXT = "This bot is created to demonstrate Spring capabilities. \n\n" +
"You can execute commands from the main menu on the left or by typing a command: \n\n" +
"Type /start to see a welcome message\n\n" +
"Type /mydata to see data stored about yourself\n\n" +
"Type /help to see this message again";
public TelegramBot(BotConfig config) {
this.config = config;
List<BotCommand> listofCommands = new ArrayList<>();
listofCommands.add(new BotCommand("/start", "get a welcome message"));
listofCommands.add(new BotCommand("/mydata", "get your data store"));
listofCommands.add(new BotCommand("/deletedata", "delete data"));
listofCommands.add(new BotCommand("/help", "info how to use this bot"));
listofCommands.add(new BotCommand("/settings", "set your preferences"));
try {
this.execute(new SetMyCommands(listofCommands, new BotCommandScopeDefault(), null));
} catch (TelegramApiException e) {
log.error("Errir setting bot`s command list: " + e.getMessage());
}
}
}
bot.name=JavaDemoTestBot
bot.token=***
#db related setting
spring.jpa.hibernate.dll-auto=update;
spring.dataasourc.url=jdbc:mysql//localhost:3306/trk
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.trakivanBot</groupId>
<artifactId>trakivanBot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>trakivanBot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>20</java.version>
<telegram.version>5.6.0</telegram.version>
</properties>
<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>${telegram.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
В userRepository прилетает null.
Подробный текст ошибки:
Field userRepository in ru.trakivanBot.trakivanBot.service.TelegramBot required a bean of type 'ru.trakivanBot.trakivanBot.model.UserRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'ru.trakivanBot.trakivanBot.model.UserRepository' in your configuration.
Пробовал различные варианты со stackoverflow и ни один вариант не помог.
Буду благодарен, если поможете