일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 오라클오류
- 스토리지기본
- import안될때
- 이미지누끼
- 게시판댓글
- api문서만들기
- 타임존설정
- jdbc연결안됨
- 크리스탈레포트그림
- openaddressing
- 크리스탈리포트이미지
- JDBC
- 타임존
- @RunWith
- git
- lombok
- fcmwebpush
- Ajax
- 크리스탈레포트누끼
- 크리스탈레포트이미지
- 추상클래스
- ResponseBody
- IT기본지식
- RequestBody
- 자료구조
- 서버기본
- 롬복
- 서블릿용어
- 크리스탈리포트이미지삽입
- EC@
- Today
- Total
엠마의 개발공부일지
싱글톤(Singleton) 본문
[싱글톤의 필요성]
: 웹개발의 경우 다수의 요청을 받아서 처리해야한다. 요청마다 새 객체를 만든다면 낭비가 많은 코드일것이다.
그래서 1개의 객체를 만들어 공유해서 쓰는 싱글톤방식을 써야한다.
[싱글톤 패턴]
- static영역에 객체 1개를 생성
- public영역에 readonly메서드를 사용하여 공유하여 사용
- private영역에 생성자를 사용하여 새로 객체생성못하게!
[싱글톤 컨테이너]
: spring container는 객체 인스턴스를 싱글톤으로 관리.
: spring container를 사용하므로써 고객요청이 새 객체를 만드는게아닌, 이미만들어진 객체를 활용하게 함
(스프링컨테이너에서 만들어진 객체를 sout해보면 같은객체가 만들어진것을 알 수 있음)
[싱글톤 사용방법]
- 스프링설정정보에 @configuration적용 -> 스프링설정정보에 "바이트코드조작라이브러리를 스프링빈으로 설정됨"
- @Bean적용하여 스프링빈에 등록
* 유의사항 *
@Bean만 적용하고 @configuration적용안하면 싱글톤처리가안된상태로 스프링빈에 등록됨!
(@configuration + @Bean = 셋뚜셋뚜!)
[참고자료]
- 요즘 xml방식 빈 등록을 많이 안하긴하지만, 참고만 하려고함 -
1.5.1. The Singleton Scope
Only one shared instance of a singleton bean is managed, 싱글톤 빈의 공유인스턴스 하나만 관리된다.
and all requests for ~에대한 beans with an ID or IDs that match that bean definition result in that one specific bean instance being returned by the Spring container.
빈 정의와 일치하는 id값을 가진 콩에대한 모든 요청으로, 하나의 빈인스턴스가 스프링컨테이너로부터 반환된다.
To put it another way, when you define a bean definition and it is scoped as a singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition.
다시말해서, 빈정의를 할때 그 범위는 싱글톤이며, 스프링컨테이너는 빈정의로부터 정의된 하나의 인스턴스를 만든다.
This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object. The following image shows how the singleton scope works:
Spring’s concept of a singleton bean differs from the singleton pattern as defined in the Gang of Four (GoF) patterns book. The GoF singleton hard-codes the scope of an object such that one and only one instance of a particular class is created per ClassLoader. The scope of the Spring singleton is best described as being per-container and per-bean. This means that, if you define one bean for a particular class in a single Spring container, the Spring container creates one and only one instance of the class defined by that bean definition. The singleton scope is the default scope in Spring. To define a bean as a singleton in XML, you can define a bean as shown in the following example:
<bean id="accountService" class="com.something.DefaultAccountService"/>
<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
[출처] 인프런_스프링핵심원리(기본편)
[출처] docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core
'Stack > Spring & Springboot' 카테고리의 다른 글
메일로 이미지 발송하기 (0) | 2020.12.22 |
---|---|
스프링으로 메일발송하기 (0) | 2020.12.22 |
스프링 웹 개발기초 (0) | 2020.12.01 |
: cmd로 스프링 빌드하기 (0) | 2020.11.27 |
여러개 테이블을 스프링으로 가져올때 (0) | 2020.11.19 |