【发布时间】:2020-07-21 09:43:03
【问题描述】:
我正在使用 Spring boot 2.3.1 和 Kotlin 1.3.72。几天前我已经更新了它。从那一刻起,我就遇到了依赖注入的问题。我不使用@Service、@Component 等注解,我更喜欢通过@Configuration 类创建bean,所以例如我有这种配置:
@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)
}
}
MatchFacade 有一个事务方法,其中所有依赖项都是null。
我知道由于CGLIB 代理,我可以将所有外观和方法设置为open,但我在pom.xml 中有标准的spring boot 构建配置:
<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>
为什么它在没有开放类和方法的情况下不起作用?
【问题讨论】:
-
因为例如 transactional 会为您的类创建一个代理,所以类和公共方法不能是最终的。无论如何,您可以将全开放插件用于所需的注释,例如
@Trasactional。 -
但我用过:kotlin-maven-allopen
标签: spring spring-boot kotlin cglib