加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:41:16 所属栏目:大数据 来源:网络整理
导读:这是userService类,它需要一个无法找到的类型为com.example.repository.userRepository的bean package com.example.services;import javax.transaction.Transactional;import org.springframework.beans.factory.annotation.Autowired;import org.springfram

这是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类

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读