스프링을 사용하면서 싱글톤 패턴이든, 스프링 같은 싱글톤 컨테이너를 사용하든, 객체 인스턴스를 하나만 생성해서 공유하는 싱글톤 방식은 여러 클라이언트가 하나의 같은 객체 인스턴스를 공유하기 때문에 싱글톤 객체는 상태를 유지(stateful)하게 설계하면 안된다.
무상태(stateless)로 설계?
- 무상태 설계
- 특정 클라이언트에 의존적인 필드가 있으면 안된다.
- 특정 클라이언트가 값을 변경할 수 있으면 안된다.
- 가급적 읽기만 가능해야 한다.
- 필드 대신에 자바에서 공유되지 않는 지역변수 파라미터, ThreadLocal 등을 사용해야 한다.
- 스프링 빈의 필드에 공유 값을 설정하면 정말 큰 장애가 발생할 수 있다.
상태를 유지할 경우 발생하는 문제점 예시
public class StatefulService {
private int price // 상태를 유지하는 필드
public void order(String name, int price) {
System.out.println("name = " + name = " price = " + price);
this.price = price; // 문제 발생
}
public int getPrice() {
return price;
}
}
상태를 유지할 경우 발생하는 문제점 예시
package hello.core.singleton;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
public class StatefulServiceTest {
@Test
void statefulServiceSingleton() {
ApplicationContext ac = new
AnnotationConfigApplicationContext(TestConfig.class);
StatefulService statefulService1 = ac.getBean("statefulService",
StatefulService.class);
StatefulService statefulService2 = ac.getBean("statefulService",
StatefulService.class);
//ThreadA: A사용자 10000원 주문 statefulService1.order("userA", 10000); //ThreadB: B사용자 20000원 주문 statefulService2.order("userB", 20000);
//ThreadA: 사용자A 주문 금액 조회
int price = statefulService1.getPrice();
//ThreadA: 사용자A는 10000원을 기대했지만, 기대와 다르게 20000원 출력 System.out.println("price = " + price);
Assertions.assertThat(statefulService1.getPrice()).isEqualTo(20000);
}
static class TestConfig {
@Bean
public StatefulService statefulService() {
return new StatefulService();
}
} }
- ThreadA가 사용자A 코드를 호출하고 ThreadB가 사용자B 코드를 호출한다.
- statefulService의 price 필드는 공유되는 필드인데, 특정 클라이언트가 값을 변경한다.
- 사용자A의 주문금액은 10000원아 되어야 하는데 20000원이라는 결과가 나온다.
- 스프링 빈은 항상 무상태(stateless)로 설계하자.
'개발공부 > Spring' 카테고리의 다른 글
좋은 객체지향프로그래밍이란? - SOLID (0) | 2024.04.10 |
---|---|
[Spring] 좋은 객체 지향 프로그래밍이란? [다형성?] (0) | 2024.04.09 |
[Spring] Object Mapper (0) | 2022.02.15 |
[Spring] AOP (0) | 2022.02.14 |
[Spring] IoC (Inversion of Control), DI (0) | 2022.02.10 |