user-icon Balazs Baltringer
04. December 2018
timer-icon 2 min

Spring Boot ConfigurationProperties on Base Classes in Kotlin

Spring Boot offers an annotation called @ConfigurationProperties. It supports the developer with an easy binding of class properties to values contained in a properties file.


Lets start with the properties file. If you have created the project with Spring Initializr then the should already be there in the /src/main/resources folder.

Add the following two lines to the file.


Create a new Kotlin file and name it ApplicationProperties.kt.


Now we add a simple RestController class to access the properties via a REST-Service.

That’s pretty straight forward!

Call the endpoint

If calling the endpoint we got the expected result.

You can also execute the provided ConfigurationPropertiesApplicationTests class.

Code continues to evolve

As we continue to develop the codebase we also want to add little more functionality and checks. We want to ensure that the configured repositoryUrl has a minimum length of 4 characters and the baseUrl is a URL.
Because of the we add the @Validated annotation on the GitHubProperties class, add the @Length annotation on the repositoryUrl property and the @URL annotation on the baseUrl.

Calling the endpoint again, we got an unexpected error from our application.

If checking the console we can see a kotlin.UninitializedPropertyAccessException: lateinit property baseUrl has not been initialized.


What happened here? It seems that the baseUrl is not set any more. But why?
Let’s go deeper and debug the code. Set a breakpoint on the return value line of the getGitHubUrlmethod in DefaultRestController class.
Start the debugger and call the endpoint again. The debugger stops at the desired line and we can analyze our injected GitHubProperties instance.

We can see that the gitHubProperties instance is of type GitHubProperties$$EnhancerBySpringCGLIB$$…..@…... It seems that adding the @Validatedannotation leads to that behavior. Spring wraps our class into a CglibAopProxy.
If we decompile the code of ApplicationProperties and check what Kotlin is doing with our property lateinit var baseUrl: String than we can see that the setter and getter are final.

It looks like that the Cglib-Proxy cannot access the baseUrl property because getter and setter are final.


The solution to this problem is quite simple.

We only need to open the baseUrl property and allow the methods to be overwritten.

If we call the REST-Endpoint again, everything works fine.

A working example of the code used in this post is available on GitHub.

Comment article