0

I'm using Spring boot 2.3.1 and Kotlin 1.3.72. I've updated it several days ago. Since that moment I've had problem with dependency injection. I don't use @Service, @Component etc. annotations, I rather prefer creating beans by @Configuration class, so for example I have this kind of configuration:

@Configuration
open class MatchModule {

    @Bean
    open fun matchFacade(
            matchQueryService: MatchQueryService,
            purchaseFacade: PurchaseFacade,
            rankFacade: RankFacade,
            timeService: TimeService
    ): MatchFacade {
        return MatchFacade(
                timeService = timeService,
                matchQueryService = matchQueryService,
                purchaseFacade = purchaseFacade,
                rankFacade = rankFacade
        )
    }

    @Bean
    open fun matchQueryService(sparringViewRepository: SparringViewRepository, animalProfileMatchViewRepository: AnimalProfileMatchViewRepository): MatchQueryService {
        return MatchQueryService(sparringViewRepository, animalProfileMatchViewRepository)
    }
}

The MatchFacade has a transactional method and inside it all dependencies are null. I know that I can make all facades and methods as open because of CGLIB proxy, but I have standard spring boot build config in pom.xml:

<build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <artifactId>kotlin-maven-plugin</artifactId>
                <groupId>org.jetbrains.kotlin</groupId>
                <version>${kotlin.version}</version>
                <configuration>
                    <compilerPlugins>
                        <plugin>jpa</plugin>
                    </compilerPlugins>
                    <jvmTarget>1.8</jvmTarget>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-noarg</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
                <!--...-->
            </plugin>
        </plugins>
    </build>

Why it doesn't work wihout open class and methods ?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Bambelal
  • 179
  • 1
  • 2
  • 13
  • because for example transactional creates a proxy to your class, so class and public methods must not be final. anyway you can use all-open plugin for the deseired annotations, for example `@Trasactional`. – zlaval Jul 21 '20 at 10:32
  • but I used it: kotlin-maven-allopen – Bambelal Jul 21 '20 at 10:51

1 Answers1

0

Here is the working solution for Spring, I think the compilerPlugins is a missing section

<plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <version>${kotlin.version}</version>
    <configuration>
        <compilerPlugins>
            <plugin>spring</plugin>
            <plugin>jpa</plugin>
        </compilerPlugins>
        <jvmTarget>${java.version}</jvmTarget>
        <args>-java-parameters</args>
    </configuration>
    <executions>
        <execution>
            <id>compile</id>
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>test-compile</id>
            <phase>process-test-sources</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-noarg</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
</plugin>
Ihar Sadounikau
  • 741
  • 6
  • 20