微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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类

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐