Chapter 12. The Spring Framework

The Spring framework is one of the most popular open source frameworks in the Java world. Spring is all about your project’s infrastructure. You focus on writing beans that contain the business logic you need for your goals, and Spring provides services like security, transactions, resource pooling, and more, based on metadata you provide.

Spring has always been friendly with “alternative” languages on the JVM. Spring has had support for Groovy since at least version 2.5. In the past few versions, the developers of Spring have added capabilities unique to Kotlin as well.

This chapter contains a few select techniques you can use in Spring applications when writing your code in Kotlin. Spring support for Kotlin is still evolving, but this chapter should give you a sense of how Kotlin figures into the Spring ecosystem.

12.1 Opening Spring-Managed Bean Classes for Extension


Spring needs to generate proxies by extending your classes, but Kotlin classes are closed (final in Java) by default.


Add the Spring plug-in for Kotlin to your build file, which automatically opens the needed Spring-managed classes for extension.


Spring works by providing services to your system. It does so through the Proxy design pattern. The Proxy design pattern, illustrated in UML class and sequence diagrams, is shown in Figure 12-1.

Proxy UML diagram
Figure ...

Get Kotlin Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.