Coder Social home page Coder Social logo

blog-springboot-confprop-cglibproxy-kotlin's Introduction

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.

Application-Properties

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

Add the following two lines to the file.

urlconfig.baseUrl=https://github.com
urlconfig.repositoryUrl=reponame

Configuration-Properties

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

@Component
@ConfigurationProperties("urlconfig")
class GitHubProperties : BaseUrlProperties() {
    lateinit var repositoryUrl: String
}

abstract class BaseUrlProperties {
    lateinit var baseUrl: String
}

RestController

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

@RestController
class DefaultRestController(private val gitHubProperties: GitHubProperties) {

    @GetMapping("/githuburl")
    fun getGitHubUrl(): String {
        return "${gitHubProperties.baseUrl}/${gitHubProperties.repositoryUrl}"
    }
}

That's pretty straight forward!

Call the endpoint

If calling the endpoint we got the expected result.

http://localhost:8080/githuburl --> https://github.com/reponame

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 ConfigurationProperties class, add the @Length annotation on the repositoryUrl property and the @URL annotation on the baseUrl.

@Component
@ConfigurationProperties("urlconfig")
@Validated
class GitHubProperties : BaseUrlProperties() {
    
    @Length(min = 4)
    lateinit var repositoryUrl: String
}

abstract class BaseUrlProperties {
    
    @URL
    lateinit var baseUrl: String
}

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

http://localhost:8080/githuburl --> There was an unexpected error (type=Internal Server Error, status=500).
                                    lateinit property baseUrl has not been initialized

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

Analyze

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 getGitHubUrl method 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 @Validated annotation 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.

Solution

The solution to this problem is quite simple.

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

@Component
@ConfigurationProperties("urlconfig")
@Validated
class GitHubProperties : BaseUrlProperties() {

    @Length(min = 4)
    lateinit var repositoryUrl: String
}

abstract class BaseUrlProperties {

    @URL
    open lateinit var baseUrl: String
}

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

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

blog-springboot-confprop-cglibproxy-kotlin's People

Contributors

balazsatwork avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.