【问题标题】:Unable to solve error of Springboot gradle - required a bean of type 'com.myapp.services.UserDetailRepository' that could not be found无法解决 Springboot gradle 的错误 - 需要找不到类型为“com.myapp.services.UserDetailRepository”的 bean
【发布时间】:2019-05-19 00:20:23
【问题描述】:

当我将@Autowired 用于我的JPA repository 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(私有仓库重命名)

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);
  }
}

休息控制器

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

    @Autowired
    private UserDetailRepository userDetailRepository;

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

UserDetailRepositoryJPA 存储库

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;

    .........

}

【问题讨论】:

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


    【解决方案1】:

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

    @EnableJpaRepositories("com.myapp.services")
    

    【讨论】:

    • 工作中...!谢谢@Alien
    猜你喜欢
    • 2018-12-20
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多