java – Spring引导说它需要一个特定的bean

前端之家收集整理的这篇文章主要介绍了java – Spring引导说它需要一个特定的bean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这是userService类,它需要一个无法找到的类型为com.example.repository.userRepository的bean

package com.example.services;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.modal.User;
import com.example.repository.userRepository;


@Service
@Transactional
public class UserService {

 @Autowired
 private userRepository userRepository;


 public UserService() {
    super();
}

public UserService(userRepository userRepository)
 {
     this.userRepository = userRepository;
 }

 public void saveMyuser(User user) {
     userRepository.save(user);
 }
}

错误消息显示

Consider defining a bean of type 'com.example.repository.userRepository' in your configuration.

这是存储库:

package com.example.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


import com.example.modal.User;


public interface userRepository extends CrudRepository

这是应用程序类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
public class TutorialProjectApplication {

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

}

最佳答案
似乎userRepository接口不在spring-boot默认扫描之外,即该存储库接口的包与使用@SpringBootApplication注释的类的子包不同.如果是这样,您需要在主类上添加@EnableJpaRepositories(“com.example.repository”).

更新:
查看更新后的帖子后,需要将@EnableJpaRepositories(“com.example.repository”)添加到TutorialProjectApplication类

原文链接:https://www.f2er.com/spring/431610.html

猜你在找的Spring相关文章