“org.springframework.beans.factory.UnsatisfiedDependencyException”的原因是什么?

时间:2018-03-12 14:58:33

标签: java spring spring-data-jpa

我遇到一个问题,Spring发现存储库时,春天应用程序停止之前会在日志中抛出此警告并抛出另一个异常:

投掷错误前的警告:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'UserREST': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mycom.mobile.respository.userRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=userRepository)}

春季申请停止时抛出错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userRepository in com.mycom.mobile.controller.UserREST required a bean of type 'com.mycom.mobile.respository.UserRepository' that could not be found.


Action:

Consider defining a bean of type 'com.mycom.mobile.respository.userRepository' in your configuration.

实体

package "com.mycom.mobile.model";

Entity
@Table(name="users")
public class User implements Serializable {

    private static final long serialVersionUID = -3009157732242241606L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @Column(name="userName")
    private String userName;

    @Column(name="userLevel")
    private String userLevel;

    @Column(name="userCountry")
    private String userCountry;

    protected User(){

    }

    public User(String userName, String userLevel, String userCountry){
        this.userName = userName;
        this.userLevel = userLevel;
        this.userCountry = userCountry;
    }

    public long getId() {
        return id;
    }

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

    public long getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName= userName;
    }

    public long getUserLevel() {
        return this.userLevel;
    }

    public void setUserLevel(String userLevel) {
        this.userLevel= userLevel;
    }

    public long getUserCountry() {
        return this.userCountry;
    }

    public void setUserCountry(String userCountry) {
        this.userLevel= userCountry;
    }
}

存储库

package "com.mycom.mobile.repository";     

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

}

控制器

package "com.mycom.mobile.controller";    

@RestController
@CrossOrigin(origins = "*")
@RequestMapping(value = "v1/api")
public class UserREST {


    @Autowired
    //@Qualifier("userRepository")
    private UserRepository userRepository;

    private Logger logger = Logger.getLogger(this.getClass());

    UserREST() {

    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ResponseEntity<Object> addUser(@RequestBody User user) {
        userRepository.save(user);
        List<User> users = Lists.newArrayList(userRepository.findAll());
        return new ResponseEntity<Object>(users, HttpStatus.OK);
    }

}

主要春季申请

package "com.mycom.mobile.UserService";

@SpringBootApplication()
// Search for any controllers that can be found under the below package ..
@ComponentScan(basePackages={"com.mycom.mobile"})
@EnableJpaRepositories(basePackages={"com.mycom.mobile.repository"})
@EntityScan(basePackages= {"com.mycom.mobile.model"})
public class UserServiceApplication {

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

    public static String getRootPath() {
        File file = new java.io.File(".");
        try {
            return file.getCanonicalPath();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

}

正如我从这里读到的那样,试图解决它但仍然遇到同样的问题: http://www.baeldung.com/spring-nosuchbeandefinitionexception#cause-2

尝试添加@Qualifier注释,但没有运气。

你能告诉我在这里做错了什么,或者为什么即使我在应用程序的配置中定义了存储库,也没有找到存储库?

修改 我在下面的链接中尝试了答案,但仍面临问题:

No qualifying bean of type found for dependency in Spring Boot single table

堆栈跟踪 https://pastebin.com/XTbiT0Dj

1 个答案:

答案 0 :(得分:0)

您可能需要在userRepository课程中为UserREST添加一个setter。

相关问题