-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton
More file actions
179 lines (133 loc) · 6.1 KB
/
singleton
File metadata and controls
179 lines (133 loc) · 6.1 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package hello.core.singleton;
import hello.core.AppConfig;
import hello.core.member.MemberService;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
public class SingletonTest {
@Test
@DisplayName("스프링 없는 순수한 DI 컨테이너")
void pureContainer() {
AppConfig appConfig = new AppConfig();
//1.조회 : 호출할 때마다 객체를 생성
MemberService memberService1 = appConfig.memberService();
MemberService memberService2 = appConfig.memberService();
//참조값이 다른 것을 확인 = 결과적으로 다른 객체가 생성됨.
System.out.println("memberService1 = " + memberService1);
System.out.println("memberService2 = " + memberService2);
//memberService1 != memberService2
Assertions.assertThat(memberService1).isNotSameAs(memberService2);
}
}
// SingletonTest.java
package hello.core.singleton;
public class SingletonService {
//자바가 뜰 때 static 영역에 자기 자신을 생성해서 instance에 참조를 넣어놓음.
private static final SingletonService instance = new SingletonService();
//이 메소드를 호출하면 항상 같은 인스턴스(1개)를 반환함.
public static SingletonService getInstance(){
return instance;
}
//외부에서 임의로 new로 객체를 생성할 수 없도록 private 생성자 사용
private SingletonService(){
}
//임의로 작성한 로직
public void logic(){
System.out.println("싱글톤 객체 로직 호출");
}
}
//SingletonService.java
@Test
@DisplayName("싱글톤 패턴을 적용한 객체 사용")
void singletonServiceTest(){
//new SingletonService(); : 컴파일 오류
SingletonService singletonService1 = SingletonService.getInstance();
SingletonService singletonService2 = SingletonService.getInstance();
System.out.println("singletonService1 = " + singletonService1);
System.out.println("singletonService2 = " + singletonService2);
assertThat(singletonService1).isSameAs(singletonService2);
}
//SingletonTest.java
@Test
@DisplayName("스프링 컨테이너와 싱글톤")
void springContainer(){
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
//1.조회 : 호출할 때마다 객체를 생성
MemberService memberService1 = ac.getBean("memberService", MemberService.class);
MemberService memberService2 = ac.getBean("memberService", MemberService.class);
//참조값이 다른 것을 확인 = 결과적으로 다른 객체가 생성됨.
System.out.println("memberService1 = " + memberService1);
System.out.println("memberService2 = " + memberService2);
//memberService1 == memberService2
Assertions.assertThat(memberService1).isSameAs(memberService2);
}
//SingletonTest.java
package hello.core.singleton;
public class StatefulService {
//stateful 해결법
public int order(String name, int price){
System.out.println("name = " + name + " price = " + price);
return price;
}
}
//StatefuleService.java
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;
class StatefulServiceTest {
@Test
void statefulServiceSingleton(){
ApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class);
StatefulService statefulService1 = ac.getBean(StatefulService.class);
StatefulService statefulService2 = ac.getBean(StatefulService.class);
//ThreadA : A 사용자가 10000원 주문
int userA = statefulService1.order("userA", 10000);
//ThreadB : B 사용자가 20000원 주문
int userB = statefulService2.order("userB", 20000);
Assertions.assertThat(statefulService1.order("userA", 10000)).isEqualTo(10000);
Assertions.assertThat(statefulService2.order("userB", 20000)).isEqualTo(20000);
}
//테스트용 설정 정보
static class TestConfig{
@Bean
public StatefulService statefulService(){
return new StatefulService();
}
}
}
//StatefuleServiceTest.java
package hello.core.singleton;
import hello.core.AppConfig;
import hello.core.member.MemberRepository;
import hello.core.member.MemberServiceImpl;
import hello.core.order.OrderServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
public class ConfigurationSingletonTest {
@Test
void configurationTest(){
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
MemberServiceImpl memberService
= ac.getBean("memberService", MemberServiceImpl.class);
OrderServiceImpl orderService
= ac.getBean("orderService", OrderServiceImpl.class);
MemberRepository memberRepository
= ac.getBean("memberRepository", MemberRepository.class);
MemberRepository memberRepository1 = memberService.getMemberRepository();
MemberRepository memberRepository2 = orderService.getMemberRepository();
System.out.println("MemberService -> memberRepository = " + memberRepository1);
System.out.println("OrderService -> memberRepository = " + memberRepository2);
System.out.println("memberRepository = " + memberRepository);
assertThat(memberService.getMemberRepository()).isSameAs(memberRepository);
assertThat(orderService.getMemberRepository()).isSameAs(memberRepository);
}
}
//ConfigurationSingletonTest.java