java 如何在配置中定义bean

pn9klfpd  于 2023-04-10  发布在  Java
关注(0)|答案(4)|浏览(111)

我的Sping Boot 应用程序遇到了一个问题。当尝试运行应用程序时,我收到错误消息“Parameter 0 of constructor in com.task_manager.backend.appController required a bean of type 'com.task_manager.backend.TaskRepository' that could not be found.”有人能建议解决这个问题吗?
错误信息:说明: com.task_manager.backend.appController中构造函数的参数0需要类型为“com.task_manager.backend.TaskRepository”的Bean,但找不到该Bean。 行动: 考虑在配置中定义一个类型为“com.task_manager.backend.TaskRepository”的bean。
我已经检查了我的代码,并确保TaskRepository接口被正确定义和导入。我还检查了应用程序属性文件,并确保数据库连接详细信息正确。
Controller.java

package com.task_manager.backend;

import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Data
public class appController {

    private final UserRepository userRepository;

    @GetMapping("Users")
    public List<User> getUsers(){
        return userRepository.findAll();
    }
    @PostMapping("addUser")
    public User addUser(@RequestBody User user){
        return  userRepository.save(user);
    }

}

主要

@SpringBootApplication
public class BackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);

    }
}

User.java

package com.task_manager.backend;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    @Id
    @GeneratedValue(strategy =  GenerationType.AUTO)
    private Long id;
    private String name;
    private String email;
    private String password;



}

UserRepository.java

package com.task_manager.backend;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}

注意:同一个包中的所有文件

vcirk6k6

vcirk6k61#

你需要像这样在appController中添加一个构造函数方法“

public appController (UserRepository userRepository){
        this.userRepository= userRepository;
    }


或者在appController类的上方添加注解“@AllArgsConstructor”

trnvg8h3

trnvg8h32#

将主类BackendApplication从

package com.task_manager.backend

package com.task_manager

并确保您的控制器具有此代码

@RestController
 public class appController {

    private UserRepository userRepository;

    @Autowired
    public EmployeeRestController(UserRepository theUserRepository ) {
        userRepository = theUserRepository;
    }

    @GetMapping("/users")
    public List<User> getUsers(){
        return userRepository.findAll();
    }

    @PostMapping("/addUser")
    public User addUser(@RequestBody User user){
        return  userRepository.save(user);
        }

 }
gk7wooem

gk7wooem3#

在私有最终UserRepository中添加@Autowired;

package com.task_manager.backend;

import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Data
public class appController {

    @Autowired
    private final UserRepository userRepository;

    @GetMapping("Users")
    public List<User> getUsers(){
        return userRepository.findAll();
    }
    @PostMapping("addUser")
    public User addUser(@RequestBody User user){
        return  userRepository.save(user);
    }

}
rbpvctlc

rbpvctlc4#

首先-建议将Main Application类放置在根包中,并带有类似@SpringBootApplication的注解(@ComponentScan@EnableAutoConfiguration@Configuration的组合)。它允许Spring扫描根包和子包中的所有类。参考阅读有关此-https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.html根据此,将主类移动到根包com.task_manager,并将控制器类移动到com.task_manager.backend
现在试试这个

@RestController
@RequiredArgsConstructor
public class appController {

    private final UserRepository userRepository;

    @GetMapping("Users")
    public List<User> getUsers(){
        return userRepository.findAll();
    }
    @PostMapping("addUser")
    public User addUser(@RequestBody User user){
        return  userRepository.save(user);
    }

}
For lombok, Make sure you have enabled annotation processor in your IDE.

当使用构造函数来设置注入的属性时,您不必提供@Autowired annotation。自Spring Framework版本4.2以来,用于依赖注入的构造函数的注解是可选的。

相关问题