На промежуточных этапах приложение содержит код как Java, так и Kotlin. Однако когда я реорганизую классы сервисов и репозиториев в Kotlin, они не могут быть автоматически подключены.
Я столкнулся со следующей ошибкой:
Код: Выделить всё
Parameter 0 of constructor in org.base.server.controller.ProjectController required a bean of type 'org.base.server.service.ProjectService' that could not be found.
Код: Выделить всё
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'projectController' defined in class path resource [org/base/server/controller/ProjectController.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'org.base.server.service.ProjectService' available: expected at least 1 bean which qualifies as an autowire candidate. Dependency annotations: {}
Дополнительная информация:
Ранее (Java) Репозиторий:
Код: Выделить всё
@Repository
public interface ProjectRepository extends JpaRepository
{
Optional findByProjectId(String projectId);
Boolean existsByProjectId(String projectId);
}
Код: Выделить всё
@Repository
interface ProjectRepository : JpaRepository
{
fun findByProjectId(projectId: String): Project?
fun existsByProjectId(projectId: String): Boolean
}
Код: Выделить всё
@Service
public class ProjectService {
private final transient ProjectRepository projectRepository;
private final transient ProjectConverter projectConverter;
@Autowired
public ProjectService(ProjectRepository projectRepository, ProjectConverter projectConverter) {
this.projectRepository = projectRepository;
this.projectConverter = projectConverter;
}
@Transactional(readOnly = true)
public ProjectDtos getAllProjects() {
return new ProjectDtos()
.setProjects(projectConverter.entitiesToDtos(projectRepository.findAll()));
}
@Transactional(readOnly = true).......
}
Код: Выделить всё
@Service
class ProjectService(
@Autowired private val projectRepository: ProjectRepository,
private val projectConverter: ProjectConverter
) {
/**
* Retrieves all projects from the repository.
*
* @return [ProjectDTOs] object containing a list of all projects as DTOs.
*/
@Transactional(readOnly = true)
fun getAllProjects(): ProjectDTOs {
return ProjectDTOs(projectConverter.entitiesToDtos(projectRepository.findAll()))
}
/**
* Retrieves a project by its unique identifier (ID).
*
* @param id the unique ID of the project
* @return the [ProjectDTO] of the project
* @throws NotFoundException if no project with the given ID exists
*/
@Transactional(readOnly = true)............
}
Код: Выделить всё
@SpringBootApplication(scanBasePackages = ["org.base.server"])
class ServerApplication
fun main(args: Array) {
SpringApplication.run(ServerApplication::class.java, *args).also {
it.beanDefinitionNames.forEach(::println)
}
}
Код: Выделить всё
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id 'pmd'
id 'java'
id 'eclipse'
id 'idea'
id 'scala'
id 'checkstyle'
id 'io.gatling.gradle' version '3.9.2.1'
id 'com.github.johnrengelman.shadow' version '8.1.0'
id 'org.springframework.boot' version '3.3.3'
id 'org.openjfx.javafxplugin' version '0.0.13'
id 'io.spring.dependency-management' version '1.1.6'
id 'com.github.ben-manes.versions' version "0.46.0"
id 'org.jetbrains.kotlin.jvm' version '1.9.25'
id "org.jetbrains.kotlin.kapt" version "1.9.25"
id "org.jetbrains.kotlin.plugin.allopen" version "1.9.25"
id "org.jetbrains.kotlin.plugin.spring" version "1.9.25"
id "org.jetbrains.kotlin.plugin.jpa" version "1.9.25"
id "org.jetbrains.kotlin.plugin.noarg" version "2.1.0"
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
kotlin {
jvmToolchain(17)
}
kapt {
keepJavacAnnotationProcessors = true
}
idea {
module {
downloadJavadoc = true
downloadSources = true
}
}
repositories {
mavenCentral()
maven { url = "https://oss.sonatype.org/content/repositories/snapshots" }
}
springBoot {
mainClass.set('org.base.server.ServerApplicationKt')
}
bootJar {
mainClass = 'org.base.server.ServerApplicationKt'
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
jar {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
ext {
springBootVersion = '3.3.3'
springVersion = '6.0.6'
springOauth2Version = "2.5.2.RELEASE"
springOauth2AutoconfigureVersion = "2.6.8"
springDocVersion = '2.2.0'
lombokVersion = '1.18.26'
junit5Version = '5.9.2'
springSecurityVersion = '6.0.5'
hibernateValidatorVersion = '8.0.0.Final'
minioVersion = '8.5.10'
kotlinVersion = '1.9.25'
jacksonKotlinVersion = '2.15.4'
dateTimeVersion = '0.6.1'
}
sourceSets {
main {
kotlin {
srcDirs += 'src/main/java'
}
}
integrationTest {
java {
compileClasspath += main.output + test.output + test.compileClasspath
runtimeClasspath += main.output + test.output + test.runtimeClasspath
srcDir file('src/integrationTest/java')
}
resources.srcDir file('src/integrationTest/resources')
}
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-quartz')
implementation('org.springframework.boot:spring-boot-starter-security')
implementation('org.springframework.boot:spring-boot-starter-actuator')
implementation('org.springframework.boot:spring-boot-starter-mail')
implementation group: "org.springframework.security", name: "spring-security-config", version: springSecurityVersion
implementation('org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:' + springOauth2AutoconfigureVersion)
implementation('org.springframework.security.oauth:spring-security-oauth2:' + springOauth2Version)
runtimeOnly("org.hibernate.validator:hibernate-validator:$hibernateValidatorVersion")
implementation("io.minio:minio:$minioVersion") {
exclude group: 'org.jetbrains.kotlin'
}
// Open API spec
implementation(group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: springDocVersion)
//runtimeOnly('org.springframework.boot:spring-boot-devtools')
runtimeOnly('org.hsqldb:hsqldb')
runtimeOnly('org.liquibase:liquibase-core:4.20.0')
runtimeOnly(group: 'org.postgresql', name: 'postgresql', version: '42.5.5')
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: lombokVersion
implementation group: 'org.projectlombok', name: 'lombok', version: lombokVersion
annotationProcessor "org.springframework:spring-context-indexer:$springVersion"
runtimeOnly group: 'org.springframework', name: 'spring-aop', version: springVersion
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonKotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-datetime:$dateTimeVersion"
implementation("org.jetbrains.kotlin:kotlin-reflect")
testImplementation group: 'io.gatling.highcharts', name: 'gatling-charts-highcharts', version: '3.9.2'
implementation('org.liquibase.ext:liquibase-hibernate6:4.20.0')
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit', module: 'junit'
}
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter', version: junit5Version
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junit5Version
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junit5Version
testImplementation group: 'org.junit.platform', name: 'junit-platform-commons', version: '1.8.2'
testImplementation group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.8.2'
testImplementation group: 'org.junit.platform', name: 'junit-platform-engine', version: '1.8.2'
gatlingImplementation('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
}
allOpen {
annotation("org.springframework.stereotype.Service")
annotation("org.springframework.stereotype.Repository")
annotation("org.springframework.stereotype.Controller")
annotation("org.springframework.web.bind.annotation.RestController")
}
noArg {
annotation("org.base.server.util.GenerateZeroArgs")
}
wrapper {
gradleVersion '8.5'
}
tasks.withType(KotlinCompile).configureEach {
compilerOptions {
jvmTarget = JvmTarget.JVM_17
apiVersion = KotlinVersion.KOTLIN_1_9
languageVersion = KotlinVersion.KOTLIN_1_9
}
}
tasks.register('integrationTest', Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
useJUnitPlatform() {
excludeEngines 'junit-vintage'
}
shouldRunAfter test
}
...............
Класс службы Kotlin не может быть внедрен здесь, в Java-контроллер:
Код: Выделить всё
@CrossOrigin
@RestController
@Slf4j
public class ProjectController {
private transient ProjectService projectService = null;
public ProjectController(
ProjectService projectService
) {
this.projectService = projectService;
}..............
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... -in-a-mixe