Spring and Kotlin final classes

Nicolay Mitropolsky

It’s time for the next topic in the series of blogs on using Spring Framework with Kotlin support in IntelliJ IDEA.

Kotlin classes are final by default, but some Spring functionality requires classes to be open. For instance, `@Configuration` classes should be extendable, and methods annotated with `@Transactional` or `@Async` must also be extendable. If you don’t want to bother with it, there is the all-open plugin as well as the spring plugin for Kotlin compiler. But if you don’t like the idea of modifying Kotlin rules by compiler plugins and don’t mind an additional `open` modifier on your classes and methods, then IntelliJ IDEA offers an inspection that checks that all necessary classes and members are declared open:

KotlinConfigs

Ordinary beans in Spring don’t have to be `open`, unless they have methods with specific annotations, like `@Caching` or `@Transactional`:

KtTransactional

So if you don’t use Kotlin compiler plugins, IntelliJ IDEA will still check the openness of your classes and members to avoid problems at runtime.

More posts about Kotlin with Spring support in IntelliJ IDEA are to come. Please stay tuned!

 

Comments below can no longer be edited.

5 Responses to Spring and Kotlin final classes

  1. Eugene Petrenko says:

    October 1, 2018

    The is yet another Kotlin compiler plugin – `spring`. It may help to `open` only necessary classes for Spring and leave other classes final.

    https://kotlinlang.org/docs/reference/compiler-plugins.html#spring-support

    • Nicolay Mitropolsky says:

      October 1, 2018

      I’ve mentioned it briefly in the beginning, but maybe it is good that you wrote about it in a comment.

  2. Joerg says:

    October 1, 2018

    Is there similar support and inspections for JEE/CDI annotations ?

  3. Andrey says:

    September 18, 2019

    Where to enable this check?

Subscribe

Subscribe for updates