Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

394
Visualizações
Using lateinit properties in constructor in kotlin

How can I use lateinit properties in my class constructor:

I have a spring component that I use to setup and access a third party library like so:

@Service
class LibProxy {

    @Value("\${lib.someProperty}")
    private lateinit var someProperty: String

    final var lib: Lib

    init {
        lib = Lib(someProperty)
    }
}

This gives a

kotlin.UninitializedPropertyAccessException: lateinit property someProperty has not been initialized

How is this supposed to be done?

I would like to avoid constructs like so:

@Service
class LibProxy {

    @Value("\${lib.someProperty}")
    private lateinit var someProperty: String

    private var lib: Lib? = null

    getLib(): Lib {
        if (lib == null) {
            lib = Lib(someProperty)
        }
        return lib ?: Lib(someProperty)
    }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

As per JEY's comment. This is how it is supposed to be done:

@Service
class LibProxy(@Value("\${lib.someProperty}") private val someProperty: String) {

    final var lib: Lib

    init {
        lib = Lib(someProperty)
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

Two possibilities here:

The easy one is to replace the init block with a @PostConstruct method.  Spring will call this once, after the object has been constructed (and hence autowired values are all set).  For example:

@PostConstruct
private fun initialise() {
    lib = Lib(someProperty)
}

The other is to arrange for the autowired property to be passed in a constructor, not set as a property (as per other answers).

One approach that works well, especially if you have several configuration properties, is to have a central class storing them:

@ConfigurationProperties("lib")
class ConfigProperties {
    var someProperty = "defaultValue"
    // …and other properties…
}

This will set someProperty from a "lib.someProperty" value in a config file (or left as "defaultValue" if not present).

You can then autowire its instance in the constructor, e.g.:

class SomeService @Autowired constructor(
    private val configProperties: ConfigProperties) {

    init {
        lib = Lib(configProperties.someProperty)
    }
}

(Centralising the properties can make it easier to find what properties are available, too.)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda