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

Problem

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

Solution

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

Discussion

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.

Figure 12-1. UML diagram of the Proxy design pattern

The idea is that the proxy and the real subject ...

Get Kotlin Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.