๐ฏ ๋ชฉํ : Singleton ํจํด๊ณผ ์คํ๋ง Singleton ์ปจํฐ์๋์ ๋ํ ์ด
public class SingletonService {
//1. static ์์ญ์ ๊ฐ์ฒด๋ฅผ ํ๋๋ง ์์ฑํ๋ค.
private static final SingletonService instance = new SingletonService();
//2. public static์ผ๋ก ์ด๋ฏธ ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๋ฐํ๋ฐ๋ ๋ฐฉ์์ผ๋ก ๋ง๋ ๋ค.
public static SingletonService getInstance() {
return instance;
}
//3. ์์ฑ์๋ฅผ private์ผ๋ก ์ ์ธํด์ ์ธ๋ถ์์ new ํค์๋๋ฅผ ์ฌ์ฉํ ๊ฐ์ฒด ์์ฑ์ ๋ชปํ๊ฒ ๋ง๋๋ค.
private SingletonService() {
}
public void logic() {
System.out.println("์ฑ๊ธํค ๊ฐ์ฒด ๋ก์ง ํธ์ถ");
}
}
@Test
@DisplayName("์ฑ๊ธํค ํจํด์ ์ ์ฉํ ๊ฐ์ฒด ์ฌ์ฉ")
public void singletonServiceTest() {
//private์ผ๋ก ์์ฑ์๋ฅผ ๋ง์๋์ด. ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
//new SingletonService();
//ํธ์ถํ ๋ ๋ง๋ค ๊ฐ์ ๊ฐ์ฒด๋ฅผ ๋ฐํ
SingletonService singletonService1 = SingletonService.getInstance();
//ํธ์ถํ ๋ ๋ง๋ค ๊ฐ์ ๊ฐ์ฒด๋ฅผ ๋ฐํ
SingletonService singletonService2 = SingletonService.getInstance();
//์ฐธ์กฐ๊ฐ์ด ๊ฐ์ ๊ฒ์ ํ์ธ
System.out.println("singletonService1 = " + singletonService1);
System.out.println("singletonService2 = " + singletonService2);
// singletonService1 == singletonService2
assertThat(singletonService1).isSameAs(singletonService2);
}
// ์ถ๋ ฅ๊ฐ
singletonService1 = hello.core.singleton.SingletonService@192c3f1e
singletonService2 = hello.core.singleton.SingletonService@192c3f1e
@Test
@DisplayName("์คํ๋ง ์ปจํ
์ด๋์ ์ฑ๊ธํค")
void springContainer() {
AnnotationConfigApplicationContext ac
= new AnnotationConfigApplicationContext(AppConfig.class);
// ์กฐํ 1
// ์กฐํ 2
MemberService memberService1 = ac.getBean("memberService", MemberService.class);
MemberService memberService2 = ac.getBean("memberService", MemberService.class);
// ์ฐธ์กฐ๊ฐ ํ์ธ
System.out.println("memberService1 = " + memberService1);
System.out.println("memberService2 = " + memberService2);
// =
Assertions.assertThat(memberService1).isSameAs(memberService2);
}
//์ถ๋ ฅ๊ฐ
memberService1 = hello.core.member.MemberServiceImpl@52d239ba
memberService2 = hello.core.member.MemberServiceImpl@52d239ba
// StatefulService.java
public class StatefulService {
private int price;
public int order(String name, int price) {
System.out.println("name = " + name+ " price = "+ price);
this.price = price;
}
public int getPrice() {
return price;
}
}
// StatefulServiceTest.java
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์ฃผ๋ฌธ
statefulService1.order("userA",10000);
// ThreadA B ์ฌ์ฉ์ 20000์ฃผ๋ฌธ
statefulService2.order("userB",20000);
//A์ ์ฃผ๋ฌธ ๊ธ์ก ์กฐํ
int price = statefulService1.getPrice();
System.out.println("price = " + price); // 20000
// ๊ฐ์ ๊ฐ์ฒด๊ธฐ ๋๋ฌธ์ 10000์์ผ๋ก ํ ๋น๋์๋ price์ ๊ฐ์ด 20000์ผ๋ก ๋ฐ๋
Assertions.assertThat(statefulService1.getPrice()).isEqualTo(20000);
}
static class TestConfig {
@Bean
public StatefulService statefulService() {
return new StatefulService();
}
}
}
Assertions.assertThat(statefulService1.getPrice()).isEqualTo(20000);
public class StatefulService {
public int order(String name, int price) {
System.out.println("name = " + name+ " price = "+ price);
return price;
}
}
@Configuration
public class AppConfig {
@Bean
public MemberService memberService() {
System.out.println("Call AppConfig.memberService");
return new MemberServiceImpl(memberRepository());
}
@Bean
public MemberRepository memberRepository() {
System.out.println("Call AppConfig.memberRepository");
return new MemoryMemberRepository();
}
@Bean
public OrderService orderService() {
System.out.println("Call AppConfig.orderService");
return new OrderServiceImpl(
memberRepository(),
discountPolicy()
);
}
@Bean
public DiscountPolicy discountPolicy() {
return new RateDiscountPolicy();
}
}
@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("memberRepository1 = " + memberRepository1);
System.out.println("memberRepository2 = " + memberRepository2);
System.out.println("memberRepository = " + memberRepository);
AppConfig appConfig = ac.getBean(AppConfig.class);
System.out.println("appConfig = " +appConfig.getClass());
Assertions.assertThat(memberRepository1).isSameAs(memberRepository);
Assertions.assertThat(memberRepository2).isSameAs(memberRepository);
}
// ์ถ๋ ฅ๊ฐ
Call AppConfig.memberService
Call AppConfig.memberRepository
Call AppConfig.orderService
memberRepository1 = hello.core.member.MemoryMemberRepository@68044f4
memberRepository2 = hello.core.member.MemoryMemberRepository@68044f4
memberRepository = hello.core.member.MemoryMemberRepository@68044f4
appConfig = class hello.core.AppConfig$$EnhancerBySpringCGLIB$$30511a3f