In Spring, I can get Spring's objects using Aware interfaces:
@Component
class TestAware : ApplicationContextAware, EnvironmentAware {
override fun setEnvironment(environment: Environment) {
println("Server port" + environment.getProperty("server.port"))
}
override fun setApplicationContext(applicationContext: ApplicationContext) {
println("ApplicationContext" + applicationContext.displayName)
}
}
But then I can do the same thing using @Autowired:
@Component
class AutowiredTest {
@Autowired
fun constructor(env: Environment, appCtx: ApplicationContext) {
println("ApplicationContext From Autowired" + appCtx.displayName)
println(env.getProperty("server.port"))
}
}
So what is the difference between them, and in which cases I must use Aware but not @Autowired?