Home > Back-end >  Could you tell me the @autowired why not?
Could you tell me the @autowired why not?

Time:04-26

I have a piece of code:

@ Component
The class TestClass: InitializingBean, DisposableBean {

The @autowired
Private lateinit var testBean: SomeObject

Override fun afterPropertiesSet () {
The log. The info (" 1: testBean $testBdean ")
}

Fun testFunction (testName: String) : Boolean {
The log. The info (" 2: testBean $testBdean ")
}

@ Throws (Exception: : class)
Override fun destroy () {

}
}

First testBean 1 a success, but the second testBean 2 shows error: lateinit property testBean from then the initialized. That is to say testBean in afterPropertiesSet () had initial success, but other functions don't have to use it?

Someone suggested that I put testBean constructor, such as: TestClass (testBean), so that all the functions can use it, but because there are many other places to use this class, so put testBean constructor is not very convenient (for other classes are initialized testBean then to TestClass constructor),

Don't know if there is any other way to initialize the testBean? Thank you very much!

CodePudding user response:

Code kotlin ah, top

CodePudding user response:

The @autowired is spring??
  • Related