簡體   English   中英

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

[英]What is the cause of the “org.springframework.beans.factory.UnsatisfiedDependencyException”?

我在使用Spring應用程序停止並引發另一個異常之前在日志中引發此警告的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)}

Spring應用程序停止時拋出錯誤:

***************************
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注釋,但沒有運氣。

您能否告訴我我在這里做錯了什么,或者即使我在應用程序的配置中定義了存儲庫也為什么找不到存儲庫?

編輯:我已經嘗試了下面的鏈接中的答案,但仍然面臨問題:

在Spring Boot單表中找不到依賴項類型的合格Bean

Stacktrace https://pastebin.com/XTbiT0Dj

您可能需要在UserREST類中為userRepository添加一個setter。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM