Giter Club home page Giter Club logo

Comments (3)

philwebb avatar philwebb commented on April 25, 2024

@MADEINCHINA @ConstructorBinding is designed to be used only on a constructor, if there's a single constructor it shouldn't be necessary. Can you provide more details about what you're trying to do and why you want to add the annotation to the type?

from spring-boot.

hanrw avatar hanrw commented on April 25, 2024

@MADEINCHINA @ConstructorBinding is designed to be used only on a constructor, if there's a single constructor it shouldn't be necessary. Can you provide more details about what you're trying to do and why you want to add the annotation to the type?
It's will raise a problem if upgrade from 2.x version. for 2.x version ConstructorBinding support ElementType.TYPE.
which means that we can add @ConstructorBinding to a data class and the recommended way to manage application properties with Kotlin can be found here spring-boot-kotlin

@ConstructorBinding in order to be able to use read-only properties.

@ConstructorBinding
@ConfigurationProperties("blog")
data class BlogProperties(var title: String, val banner: Banner) {
  data class Banner(val title: String? = null, val content: String)
}

kotlinlang - getters-and-setters
The full syntax of a read-only property declaration differs from a mutable one in two ways: it starts with val instead of var and does not allow a setter

from spring-boot.

wilkinsona avatar wilkinsona commented on April 25, 2024

I can't see a need for @ConstructorBinding in your example. Both BlogProperties and Banner have only a single constructor so we can automatically identify the constructor to use. If you declare your data class with a secondary constructor, you can use @ConstructorBinding to identify the constructor that should be used with the help of the constructor keyword:

@ConfigurationProperties("blog")
data class BlogProperties @ConstructorBinding constructor(val title: String, val banner: Banner) {
  constructor(banner: Banner) : this("Default title", banner)
  data class Banner(val title: String? = null, val content: String)
}

Here, the constructor that takes a String and a Banner will be used for configuration property binding.

Given the above, I'm going to close this issue. If there's a situation where binding to a data class still does not work, please provide a complete yet minimal sample that reproduces the problem. We can then re-open this issue and take another look.

from spring-boot.

Related Issues (20)

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.