Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

387
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!