Today I have learned about Spring bean scope.
The most generally used scopes are Singleton, request, prototype, and etc..
Singleton
singleton scope is DEFAULT for spring bean. Most of beans we used are singleton. Picture your API server code.
@Controller, @Service, @Repository, @Component, these are singleton. As at last I have written, singleton beans are
created, registered, and injected their dependencies at the time of spring container initialization.
Prototype
Prototype bean is instantiated when it called. After it created and registered, and injected, spring call the initialization callback of it. However, spring doesn't care about the end of prototype bean. Therefore, @PreDestroy or @Bean(destroyMethod="thatMethod") would never work(Without involving of BeanPostProcessor). It removed when all references to that instance are removed.
Request
Request scope included in Spring MVC.
A request-scoped bean is created for each HTTP request connection, also destroyed the connection closed.
On my opinion, It would used for form, request logger, etc..
'Web > Spring' 카테고리의 다른 글
Unable to load class 'javax.persistence.Entity'. (0) | 2023.02.05 |
---|---|
Spring security filter vs Nest.js Guard (0) | 2023.01.30 |
스프링에서 싱글톤으로 관리하는 빈의 생성주기 (0) | 2023.01.26 |
스프링 컨테이너 (0) | 2023.01.24 |