Spring and Kotlin final classes

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!

 

This entry was posted in New Features, Tips & Tricks. Bookmark the permalink.

4 Responses to Spring and Kotlin final classes

  1. Eugene Petrenko says:

    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:

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

  2. Joerg says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *