I have a @RestController which has an injected @Service bean. I am having trouble understanding the lifespan of the Controller and its Service. Is it on a per-request basis? Or is it longer than that? I am new to Spring and how beans are managed.
@RestController
class AppController {
private final AppService appService
AppController(AppService appService) {
this.appService = appService
}
...
}
@Service
class AppService {
private final DataSource dataSource
AppService(DataSource dataSource) {
this.dataSource = dataSource
}
private Sql getSql() {
new Sql(dataSource.connection)
}
...
}
The reason I ask is because we are instantiating a sql connection within the service and I am curious if I can memoize and reuse the connection or if I will have one instance per request that needs to be closed immediately.
Spring Boot 1.5.2