用@Configuration 注释的类可以被隐式子类化,并且不能是最终的

New*_*ser 3 kotlin

我在 Kotlin 上创建了一个新项目来生成报告。知道 Kotlin 可以减少代码行数并且比 Java更安全,我使用 Spring-jpa 和 Kotlin 来完成这项工作。我所有的@Configuration课程都有一个错误:

用@Configuration 注释的类可以被隐式子类化,并且不能是最终的

仅供参考,我正在使用 Kotlin 1.3.50 的 Maven 项目。据我所知,我知道 spring 确实是用于注入值的子类。

我如何让春天快乐,但又不能在春天抱怨的每个班级中保持开放写作?

New*_*ser 5

您可能已经知道,默认情况下 kotlin 类不能扩展。为了支持继承,你必须添加open关键字。此外,我们知道 Spring 注解处理需要标记为@Configuration可以被子类化的类。

在每个类中编写 open 的替代方法是添加一个插件: kotlin-allopen

前任 :

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlin.version}</version>
    <configuration>
        <compilerPlugins>
            <plugin>spring</plugin>
        </compilerPlugins>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>




<compilerPlugins>
    <plugin>spring</plugin>
</compilerPlugins>
Run Code Online (Sandbox Code Playgroud)

上面的方法是将 open 添加到以下注释中:@Component, @Async, @Transactional,@Cacheable@SpringBootTest。由于元注释支持与注解的类@Configuration@Controller@RestController@Service@Repository自动打开,因为这些注释是元注解为@Component

您可以在此处找到文档:https : //kotlinlang.org/docs/reference/compiler-plugins.html