gpt4 book ai didi

java - 创建名称为 'mainController' : Unsatisfied dependency expressed through field 'userRepository' 的 bean 时出错

转载 作者:行者123 更新时间:2023-11-29 18:30:52 25 4
gpt4 key购买 nike

我已经遇到这个错误几个星期了,但我不知道如何修复它。 Stack Overflow 上的类似解决方案不适合我的项目。

我目前使用的是mysql数据库,但是每当尝试启动服务器时都会遇到这个问题:StackTrace

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) on project iPbackend: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.resource.iPbackend.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)

我正在使用这个主 Controller :MainController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.resource.iPbackend.UserRepository;
import com.resource.iPbackend.User;

@Controller
@RequestMapping(path = "/main")
public class MainController {

@Autowired
private UserRepository userRepository;

@RequestMapping(path = "/reg", method = RequestMethod.POST)
public @ResponseBody String regNewUser (@RequestParam String firstName, @RequestParam String lastName, @RequestParam String email, @RequestParam String password, @RequestParam String username) {
User n = new User();
n.setFirstName(firstName);
n.setLastName(lastName);
n.setEmail(email);
n.setPassword(password);
n.setUsername(username);
userRepository.save(n);
return "User is stored in database: " + n;
}

@GetMapping(path = "/all")
public @ResponseBody Iterable<User> getAllUsers() {
return userRepository.findAll();
}
}

与此存储库一起:UserRepository.java

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.resource.iPbackend.User;


@Repository
public interface UserRepository extends CrudRepository<User, Long> {

}

这个实体:User.java

import org.springframework.data.annotation.Id;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String firstName;
private String lastName;
private String email;
private String password;
private String username;

public Integer getId() {
return id;
}

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}

最佳答案

您需要在基本配置类中包含注释@EnableJpaRepositories,以便 Spring Data 将扩展 CrudRepository 的接口(interface)注册为 spring bean。

此外,注释 @Repository 在接口(interface)上不执行任何操作,因此不需要这样做,但它也不会因为存在而破坏任何内容。

编辑:

您还可以尝试通过告诉 @EnableJpaRepositories 注释您的存储库的确切位置来更加具体。即:

@EnableJpaRepositories("com.resource.iPbackened")

关于java - 创建名称为 'mainController' : Unsatisfied dependency expressed through field 'userRepository' 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45702533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com