当我将 @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/