Consider defining a bean of type in your configuration
Получил ошибку "Consider defining a bean of type in your configuration" в репозитории пользователя. Не понимаю, почему она произошла и как ее можно исправить?
Application:
package nulltiton.projectprogress;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class ProjectProgressApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectProgressApplication.class, args);
}
}
Репозиторий:
package nulltiton.projectprogress.repository;
import nulltiton.projectprogress.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<Integer, User> {
User getById(int id);
}
MainController:
package nulltiton.projectprogress.controllers;
import lombok.RequiredArgsConstructor;
import nulltiton.projectprogress.repository.UserRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
@RequiredArgsConstructor
public class MainController {
private final UserRepository userRepository;
@GetMapping({"/", "/index", ""})
public String showMainPage(Model model)
{
model.addAttribute("test", userRepository.getById(1));
return "main/index";
}
}
User:
package nulltiton.projectprogress.entity;
import jakarta.persistence.*;
import lombok.Data;
import org.springframework.data.annotation.Id;
@Entity
@Table(name = "user")
@Data
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;
@Column(length = 100, nullable = false, unique = true)
private String login;
@Column(length = 100, nullable = false)
private String password;
@Column(length = 100, nullable = false)
private String firstName;
@Column(length = 100, nullable = false)
private String lastName;
@Column(length = 100, nullable = false)
private String fatherName;
@Column(length = 100, nullable = false, unique = true)
private String email;
}
Ответы (2 шт):
Автор решения: Mihail Bykhovsky
→ Ссылка
Поменять местами
public interface UserRepository extends JpaRepository<Integer, User> {
User getById(int id);
}
на
public interface UserRepository extends JpaRepository<User, Integer> {
}
Уже есть метод
Optional<T> findById(ID id);
Автор решения: Mihail Bykhovsky
→ Ссылка
Еще вместо @RequiredArgsConstructor
Поставь
@Autowired private UserRepository userRepository;