java - 无法解决Springboot gradle错误 - 需要一个类型为 'com.myapp.services.UserDetailRepository'的bean,但找不到

标签 java spring-boot gradle spring-data-jpa

当我将 @Autowired 用于我的 JPA 存储库接口(interface) 时,我收到此错误。当我从 Maven 更改为 Gradle 时,我看到了这一点。

我在我的主类上使用了@ComponentScan,但没有运气。为什么它在 Gradle 中失败。

错误描述:

Field userDetailRepository in com.myapp.controllers.UserRestController required a bean of type 'com.myapp.services.UserDetailRepository' that could not be found.

Consider defining a bean of type 'com.myapp.services.UserDetailRepository' in your configuration.

我的 Gradle(私有(private)存储库重命名)

buildscript {
  repositories {
    maven { url "https://binrepo.private.com/artifactory/platform" }
    maven { url "https://binrepo.private.com/artifactory/jcenter" }
    maven { url "https://binrepo.private.com/artifactory/gradle" }
    maven { url "https://binrepo.private.com/artifactory/maven-central" }
    jcenter()
    mavenCentral()
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {    
    classpath "com.target.platform:platform-connector-gradle:1.1.4"
    classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.18.RELEASE"

  }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'application'
apply plugin: "com.target.platform.connector.spring-boot"

repositories {
    maven { url "https://binrepo.private.com/artifactory/platform" }
    maven { url "https://binrepo.private.com/artifactory/jcenter" }
    maven { url "https://binrepo.private.com/artifactory/gradle" }
    maven { url "https://binrepo.private.com/artifactory/maven-central" }
    jcenter()
    mavenCentral()
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }

sourceSets.main.java.srcDirs = ['src']
mainClassName = "com.myapp.Main"
sourceCompatibility = 1.8
targetCompatibility = 1.8

version = "3"

distTar.version = ""

dependencies {
    compile('org.springframework.boot:spring-boot-autoconfigure',
            'org.springframework.boot:spring-boot-starter',
            "org.springframework.boot:spring-boot-starter-web:1.5.18.RELEASE",
            "org.springframework.boot:spring-boot-starter-data-jpa:1.5.18.RELEASE",
            "org.springframework.boot:spring-boot-starter-data-ldap:1.5.18.RELEASE",
            "org.springframework.boot:spring-boot-starter-mail:1.5.18.RELEASE",
            "org.springframework.boot:spring-boot-starter-validation:1.5.18.RELEASE",
            "org.springframework.boot:spring-boot-devtools",
            "org.postgresql:postgresql",
            "it.ozimov:spring-boot-email-core:0.6.3",
            "org.freemarker:freemarker:2.3.27-incubating",
            "io.springfox:springfox-swagger2:2.9.2",
            "io.springfox:springfox-swagger-ui:2.9.2",
            "io.springfox:springfox-core:2.9.2",
            "io.springfox:springfox-spi:2.9.2")

}

configurations.all {
    exclude module:"spring-boot-starter-logging"
}

我的应用程序引导文件

@Configuration
@SpringBootApplication
@EnableAutoConfiguration
public class Main {
  public static void main(String[] args) {
    SpringApplication.run(Main.class);
  }
}

休息 Controller

@RestController
@RequestMapping("/users")
public class UserRestController {

    @Autowired
    private UserDetailRepository userDetailRepository;

    @GetMapping("/{id}")
    public UserDetail get(@PathVariable Integer id) {
         return userDetailRepository.findOne(id);
    }
}

UserDetailRepository JPA 存储库

public interface UserDetailRepository extends JpaRepository<UserDetail, Integer> {
    public UserDetail findOne(Integer id);
}

UserDetail 实体类

@Entity
@Table(name = "user_detail")
@XmlRootElement
public class UserDetail implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer userId;
    @Size(max = 25)
    @Column(name = "name")
    private String userName;

    .........

}

最佳答案

看起来 Spring Boot 无法找到存储库。因此,让 spring 了解存储库类的位置,如下所示。

@EnableJpaRepositories("com.myapp.services")

关于java - 无法解决Springboot gradle错误 - 需要一个类型为 'com.myapp.services.UserDetailRepository'的bean,但找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53835454/

相关文章:

java - @ConditionalOnBean 不适用于 JdbcTemplate.class

android - 发布 GitHub 包返回 422 错误

java - 如何使用 try catch 继续循环?

java - JSONRPC Java 实现

java - Spring Boot 在部署到 Tomcat 期间无法加载外部 jar

gradle - libgdx一段时间后无法加载纹理

java - 对于 gradle 守护进程无法与 ClearCase 一起使用该怎么办?

java - 使用类加载器获取不同项目中的资源

java - Java中重新定义一个引用(扩展使用)

java - Spring mongodb @DBRef 查询在基本查询中搜索父字段