Features Tips & Tricks

Spring Config and missing Kotlin members

Kotlin is now a first-class citizen in the Spring Framework, and in IntelliJ IDEA we are trying to make all the Spring features that have worked with Java over the years work with Kotlin too. In this series of blog posts, we will walk you through some of these features using Kotlin samples, this will also be a reminder in case you missed them for Java.

And today we’ll start with XML-configuration config support. XML-based Spring configurations are not so popular nowadays, but a large number of our users still use them, and IntelliJ IDEA can help maintain such configurations.

For instance, by having a simple Kotlin class:

package org.jetbrains.samples.kotlinspring

class KtBean

we can start using it in a Spring. XML file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="bean" class="org.jetbrains.samples.kotlinspring.KtBean"/>
</beans>

And then if you realize that you want to add an initialization method. Then you could just write it in the XML config and use a quick-fix to add it to the Kotlin class:

kt-spring-xml-add-init

Of course, you’ll still get all the navigation and completion features
It also works in a similar way with code-based configurations:

kt-spring-xml2-add-init-in-code

And in the same way it works with constructors: if you need a constructor for a bean, IntelliJ IDEA will also add them (or modify the existing one):

kt-spring-construtor1

kt-spring-construtor2

If you need a property in the Kotlin class this quick-fix will suggest creating a `lateinit var` that allows you to have uninitialized non-nullable properties, which is very handy for Spring beans:

kt-add-prop
the latter feature had some flaws in the latest releases, but everything is fixed in Kotlin 1.2.70

So if you use some non-existing members of a class in Spring-configuration – IntelliJ IDEA will help you add these members to the class, even if your class happens to be in Kotlin.

We have more posts about Kotlin with Spring support in IntelliJ IDEA coming up. Please stay tuned and check out other features for Spring in IntelliJ IDEA!

image description