Я получаю эту ошибку, когда я использую @Autowired
для моего JPA repository interface
.Я видел это, когда я изменился на Gradle
с Maven
.
Я использовал @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.
My 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"
}
Файл начальной загрузки My Application
@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);
}
}
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;
.........
}