๊ด€๋ฆฌ ๋ฉ”๋‰ด

ejyoo's ๊ฐœ๋ฐœ ๋…ธํŠธ

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์— ๋Œ€ํ•ด์„œ (Keyword : ์‹ฑ๊ธ€ํ†ค ๋””์ž์ธ ํŒจํ„ด) ๋ณธ๋ฌธ

BackEnd/Java

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์— ๋Œ€ํ•ด์„œ (Keyword : ์‹ฑ๊ธ€ํ†ค ๋””์ž์ธ ํŒจํ„ด)

ejyoovV 2021. 3. 15. 19:11

๐Ÿ’ก ๋“ค์–ด๊ฐ€๊ธฐ ์ „์—

๋ฌดํ•œ์œผ๋กœ ์กด์žฌํ•˜๋Š” ์ž์›์€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ปดํ“จํ„ฐ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ž์› ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ œํ•œ๋˜์–ด ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋‚จ์šฉ๋˜๋Š” ๊ฒƒ์€ ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š๊ณ 

ํ•˜๋‚˜์˜ ์ž์›์œผ๋กœ ๋ชจ๋‘๊ฐ€ ๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์€ ์œ ์šฉํ•œ ๋ฐฉ๋ฒ•์ด ๋  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ’ก ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด(Singleton pattern)์ด๋ž€?

์†Œํ”„ํŠธ์›จ์–ด ๋””์ž์ธ ํŒจํ„ด์—์„œ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ๋”ฐ๋ฅด๋Š” ํด๋ž˜์Šค๋Š”, ์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ ์ฐจ๋ก€ ํ˜ธ์ถœ๋˜๋”๋ผ๋„, ์‹ค์ œ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋Š” ํ•˜๋‚˜์ด๊ณ  ์ตœ์ดˆ ์ƒ์„ฑ ์ดํ›„์— ํ˜ธ์ถœ๋œ ์ƒ์„ฑ์ž๋Š” ์ตœ์ดˆ์˜ ์ƒ์„ฑ์ž๊ฐ€ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

์ฆ‰ ์ƒ์„ฑ์ž์˜ ํ˜ธ์ถœ์ด ๋ฐ˜๋ณต์ ์œผ๋กœ ์ด๋ค„์ ธ๋„ ์‹ค์ œ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋Š” ์ตœ์ดˆ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

๐Ÿ“ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋งŒ์„ ์ƒ์„ฑํ•ด ์ดํ›„์— ํ˜ธ์ถœ๋œ ๊ณณ์—์„œ๋Š” ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์ „๋ฐ˜์—์„œ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋งŒ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•จ.

๐Ÿ“ ์–ด๋–ค ํด๋ž˜์Šค๊ฐ€ ์ตœ์ดˆ ํ•œ๋ฒˆ๋งŒ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๊ณ (Static) ๊ทธ ๋ฉ”๋ชจ๋ฆฌ์— ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋Š” ๋””์ž์ธ ํŒจํ„ด์„ ์˜๋ฏธํ•จ.

 

๋””์ž์ธ ํŒจํ„ด์€ ์„ ๋ฐฐ ๊ฐœ๋ฐœ์ž ๋ถ„๋“ค์ด ๊ฐœ๋ฐœ์„ ํ•˜์‹œ๋ฉด์„œ,

์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๋ฉด ์ข‹๋‹ค ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ํŒจํ„ด์œผ๋กœ ์ •์˜ํ•œ๊ฒƒ์ด๋‹ค.

๋”ฐ๋ผ์„œ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด๋„ ๋””์ž์ธ ํŒจํ„ด ์ค‘ ํ•˜๋‚˜์ด๊ณ , ๊ผญ ์จ์•ผ๋˜๋Š”๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์ƒํ™ฉ์— ๋งž๊ฒŒ ์“ฐ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

๐Ÿ’ก ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด ์˜ˆ์‹œ

์•„๋ž˜์˜ ์˜ˆ์ œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ BoardServiceImpl ํด๋ž˜์Šค๋กœ

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด ๊ตฌํ˜„ ์‹œ ์ธํ„ฐํŽ˜์ด์Šค์ธ IBoardService๋ฅผ ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•œ ๋’ค,

BoardServiceImpl ์ƒ์„ฑ์ž ํ˜ธ์ถœ ์‹œ BoardDaoImpl๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.(๋‹คํ˜•์„ฑ ๊ฐœ๋…)

public class BoardServiceImpl implements IBoardService{
	private IBoardDao boardDao;
	private Connection conn;
	
//	์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด ์œ„ํ•œ ์ž์‹  ๊ฐ์ฒด ์ƒ์„ฑ
	private static IBoardService boardService;
	
	private BoardServiceImpl() {
		boardDao = BoardDaoImpl.getInstance();
	}
	
	public static IBoardService getInstance() {
		if(boardService == null) {
			boardService = new BoardServiceImpl();
		}
		return boardService;
	}

ํด๋ž˜์Šค ๋ณ€์ˆ˜์™€ ์ƒ์„ฑ์ž๋ฅผ ์™ธ๋ถ€์—์„œ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋„๋ก ์ œํ•œ์„ ๋‘์–ด

๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ํ•˜๋‚˜์˜ ๊ฐ์ฒด์˜ ์ฃผ์†Œ๋งŒ ์‚ฌ์šฉํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ’ก ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์˜ ์‚ฌ์šฉ?

์ฃผ๋กœ ๊ณตํ†ต๋œ ๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” DBCP(DataBase Connection Pool)๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

 

๐Ÿ’ก ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š”?

ํ•œ๋ฒˆ์˜ ๊ฐ์ฒด ์ƒ์„ฑ์œผ๋กœ ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

์‹ฑ๊ธ€ํ†ค์œผ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋Š” ๋ฌด์กฐ๊ฑด ํ•œ๋ฒˆ ์ƒ์„ฑ์œผ๋กœ ์ „์—ญ์„ฑ์„ ๋„๊ธฐ์— ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ๊ณต์œ ๊ฐ€ ์šฉ์ดํ•˜๋‹ค.

 

๐Ÿ’ก ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์˜ ๋ฌธ์ œ์ 

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์€ ์ „์—ญ์„ฑ์„ ๋„๋ฉด์„œ

๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์™€ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ์ผ€์ด์Šค์—์„œ๋งŒ ์‚ฌ์šฉํ•  ๋•Œ ํšจ์œจ์ ์ด๋ฉฐ

๊ทธ ์™ธ์—๋Š” ๋ฌธ์ œ์ ์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค.

์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๋งŒ๋“  ๊ฐ์ฒด์˜ ์—ญํ• ์ด ๊ฐ„๋‹จํ•œ ๊ฒƒ์ด ์•„๋‹Œ ์—ญํ• ์ด ๋ณต์žกํ•œ ๊ฒฝ์šฐ๋ผ๋ฉด

ํ•ด๋‹น ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ„์˜ ๊ฒฐํ•จ๋„๊ฐ€ ๋†’์•„์ ธ์„œ

๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„ ์›์น™์— ์–ด๊ธ‹๋‚˜๊ฒŒ ๋œ๋‹ค. (๊ฐœ๋ฐฉ-ํ์‡„)

ํ•ด๋‹น ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•  ๊ฒฝ์šฐ, ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ณณ์—์„œ ์‚ฌ์ด๋“œ ์ดํŒฉํŠธ ๋ฐœ์ƒํ•  ํ™•๋ฅ ์ด ์ƒ๊ธฐ๊ฒŒ ๋˜๋ฉฐ

๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ๋™๊ธฐํ™” ์ฒ˜๋ฆฌ ๋ฌธ์ œ ๋“ฑ์ด ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค.

 


๋‹ค์–‘ํ•œ ์‹ฑ๊ธ€ํ†ค์˜ ๊ตฌํ˜„

 

๐Ÿ’ก static block

public class ExampleClass1 {
	//Instance
	private static ExampleClass1 instance;
	
	//private construct
	private ExampleClass1() {}
	
	static {
		try {instance = new ExampleClass1();}
		catch(Exception e) { throw new RuntimeException(
				"Create instance fail. error mse = " + e.getMessage());}
	}
	
	public static ExampleClass1 getInstance() {
		return instance;
	}
}

์œ„์™€ ๊ฐ™์ด static ๋ธ”๋Ÿญ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ํด๋ž˜์Šค๊ฐ€ ๋กœ๋”ฉ๋  ๋•Œ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰์„ ํ•˜๊ฒŒ๋˜๋Š” ํŠน์„ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์ธ์Šคํ„ด์Šค๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” ์‹œ์ ์ด ์•„๋‹Œ ํด๋ž˜์Šค ๋กœ๋”ฉ ์‹œ์ ์— ์‹คํ–‰๋œ๋‹ค.

 

๐Ÿ’ก lazy init

์œ„ static ๋ฐฉ๋ฒ•์—์„œ ๊ฐœ์„ ํ•˜์—ฌ ํด๋ž˜์Šค ๋กœ๋”ฉ ์‹œ์ ์ด ์•„๋‹Œ ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•„์š”ํ•˜์—ฌ ์š”์ฒญํ•  ๋•Œ ์ƒ์„ฑ๋˜๋Š” ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•˜์˜€๋‹ค.

๐Ÿ“ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ private๋กœ ์ƒ์„ฑํ•˜์—ฌ ์™ธ๋ถ€์—์„œ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์„ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ณ , getInstance๋ฅผ ํ†ตํ•ด์„œ๋งŒ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค.

๐Ÿ“ getInstance๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ธฐ์กด์— ์ƒ์„ฑ๋œ ๊ฐ’์ด ์กด์žฌํ•˜๋ฉด ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ˜•ํƒœ๋กœ ํ”„๋กœ๊ทธ๋žจ ์ „๋ฐ˜์— ๊ฑธ์ณ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์œ ์ง€ํ•œ๋‹ค.

๐Ÿ“ ์ธ์Šคํ„ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ์™€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” ๋ชจ๋‘ Static์œผ๋กœ ์„ ์–ธ๋œ ์ •์  ๋ณ€์ˆ˜ ๋ฐ ๋ฉ”์„œ๋“œ์ด๋‹ค.

๐Ÿ“ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ถ€์—์„œ ์ธ์Šคํ„ด์Šค์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ๋‹น์—ฐํžˆ ํด๋ž˜์Šค ๋ณ€์ˆ˜ ๋ฐ ๋ฉ”์„œ๋“œ์— ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๋ฉ”์„œ๋“œ๋Š” ์ •์  ํƒ€์ž…์œผ๋กœ ์„ ์–ธ๋œ๋‹ค.

public class ExampleClass2 {
	//Instance
	private static ExampleClass2 instance;
	
	//private construct
	private ExampleClass2() {}
	
	public static ExampleClass2 getInstance() {
		if(instance == null) {instance = new ExampleClass2();}
		return instance;
	}
}

๐Ÿ“ ํ•˜์ง€๋งŒ ์œ„ ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑํ•  ๊ฒฝ์šฐ ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ(Multi-Thread) ํ™˜๊ฒฝ์—์„œ ์ทจ์•ฝํ•˜๋‹ค.

์—ฌ๋Ÿฌ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๊ณต์œ ๋˜๊ณ  ์žˆ๋Š” ์ƒํ™ฉ์—์„œ ์กฐ๊ฑด๋ฌธ์ด ๋™์‹œ์— ๋‘๋ฒˆ ๋Œ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 

ํŠน์ • ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— getInstance() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด ์ธ์Šคํ„ด์Šค๊ฐ€ ๋‘๋ฒˆ ์ƒ์„ฑ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ count ๊ฐ’์€ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์“ฐ๋ ˆ๋“œ์—์„œ ๊ณต์œ ํ•˜๊ณ  ์žˆ๊ณ  ์„œ๋กœ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์—์„œ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์ด ์ผ๊ด€๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

public class ExampleClass5 {
	//Instance
	private static ExampleClass5 instance;
	private int count = 0;
	
	//private construct
	private ExampleClass5() {}
	
	public static ExampleClass5 getInstance() {
		if(instance == null) {instance = new ExampleClass5();}
		return instance;
	}
	
	public void print(String input) {
		count++;
		System.out.println(input + "count : " + count);
	}
}

 

 

๐Ÿ’ก Thread safe + lazy (์ •์  ๋ณ€์ˆ˜์— ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ”๋กœ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๋ฐฉ๋ฒ•)

public class ExampleClass3 {
	//Instance
	private static ExampleClass3 instance;
	
	//private construct
	private ExampleClass3() {}
	
	public static synchronized ExampleClass3 getInstance() {
		if(instance == null) {instance = new ExampleClass3();}
		return instance;
	}
}

Lazy์—์„œ ๋ณด์˜€๋˜ getInstance() ๋ฉ”์„œ๋“œ์— synchronized ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ž„์œผ๋กœ์จ ์“ฐ๋ ˆ๋“œ์—์„œ ๋™์‹œ ์ ‘๊ทผ์— ๋Œ€ํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

ํ•˜์ง€๋งŒ synchronized ํ‚ค์›Œ๋“œ๋Š” ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

 

๐Ÿ’ก Holder

public class ExampleClass4 {
	//private construct
	private ExampleClass4() {}
	
	private static class InnerInstanceClazz() {
		private static final ExampleClass4 instance = new ExampleClass4();
	}
	
	public static ExampleClass4 getInstance() {
		return InnerInstanceClazz.instance;
	}
}

JVM์˜ ํด๋ž˜์Šค ๋กœ๋” ๋ฉ”์ปค๋‹ˆ์ฆ˜๊ณผ ํด๋ž˜์Šค์˜ ๋กœ๋“œ ์‹œ์ ์„ ์ด์šฉํ•˜์—ฌ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ ์‹œํ‚ด์œผ๋กœ์จ ์“ฐ๋ ˆ๋“œ ๊ฐ„์˜ ๋™๊ธฐํ™” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

์œ„ ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ Java์—์„œ ์‹ฑ๊ธ€ํ†ค ์ƒ์„ฑ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

ํ˜„์žฌ ๋‚ด๊ฐ€ ํ…Œ์ŠคํŠธ ํ• ๋•Œ์—๋Š” ์ € ์ฝ”๋“œ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์—ˆ๋‹ค.

ํ˜„์žฌ ์ง€์‹์œผ๋กœ๋Š” ์˜ค๋ฅ˜๋ฅผ ์žก์ง€ ๋ชปํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜์ค‘์— ์•Œ๊ฒŒ๋˜๋ฉด ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

 

๐Ÿ’ก ์ •๋ฆฌ

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์€ Spring framework ์—์„œ๋„ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋ฉฐ, ์–ด๋–ค์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š”์ง€ ์•Œ์•„๋‘๋ฉด ๋„์›€์ด ๋œ๋‹ค.

์ž๋ฐ”์™€ Spring ์—์„œ์˜ ์‹ฑ๊ธ€ํ†ค ์ฐจ์ด์ ์ด๋ผ๋ฉด, ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด์˜ ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ๋‹ค๋ฅด๋‹ค.

๋˜ํ•œ ์ž๋ฐ”์—์„œ ๊ณต์œ ๋ฒ”์œ„๋Š” Class loader ๊ธฐ์ค€์ด์ง€๋งŒ, Spring์—์„œ๋Š” ApplicationContext๊ฐ€ ๊ธฐ์ค€์ด ๋œ๋‹ค.

๐Ÿ“ ์‹ฑ๊ธ€ํ†ค์€ ํ”„๋กœ๊ทธ๋žจ ์ „์ฒด์—์„œ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋งŒ์„ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ๊ฐ์ฒด๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์•„์ ธ์„œ ๋ณ€๊ฒฝ์— ์œ ์—ฐํ•˜๊ฒŒ ๋Œ€์ฒ˜ํ•  ์ˆ˜ ์—†๋‹ค.

๐Ÿ“ ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ด๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๋ชจ๋“  ๊ฐ’๋“ค์ด ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ, ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ๋Œ€์ฒ˜๊ฐ€ ์–ด๋Š์ •๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๊ณ ๋ คํ•ด์•ผ ํ•  ์š”์†Œ๊ฐ€ ๋งŽ์•„ ์‚ฌ์šฉ์ด ์–ด๋ ต๊ณ  ํ”„๋กœ๊ทธ๋žจ ์ „๋ฐ˜์— ๊ฑธ์ณ์„œ ํ•„์š”ํ•œ ๋ถ€๋ถ„์—๋งŒ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์žฅ์ ์ด ์žˆ๋‹ค. (ํฌ์ธํŠธ๋ฅผ ์žก๊ธฐ ์–ด๋ ต๊ธด ํ•˜๋‹ค.)

๐Ÿ“ ์ ์ ˆํ•œ ํ˜•ํƒœ๋กœ ์‹ฑ๊ธ€ํ†ค์„ ํ™œ์šฉํ•˜๋ฉด ์ข‹์ง€๋งŒ ๋‚จ์šฉํ•˜๊ฒŒ ๋  ์—ฌ์ง€๊ฐ€ ๋งŽ๋‹ค.

๐Ÿ“ ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ์˜ ์‹ฑ๊ธ€ํ†ค 

- Synchronized๋ฅผ ํ†ตํ•ด ๊ด€๋ฆฌํ•˜๋ฉด ๋˜๋ฉฐ, ๋‹ค์–‘ํ•œ ๋ณ€ํ™”์— ๋Œ€์‘ํ•˜๊ธฐ ์œ„ํ•ด ์ธํ„ฐํŽ˜์ด์Šค์˜ ํ˜•ํƒœ๋กœ ๊ด€๋ฆฌํ•˜๋ฉด ์ข‹๋‹ค.

๐Ÿ“ ๋‹จ์ผ ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ์˜ ์‹ฑ๊ธ€ํ†ค

- ์ •์  ํด๋ž˜์Šค์˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. (ํด๋ž˜์Šค ๋กœ๋”ฉ๋‹จ๊ณ„์—์„œ ๋ฐ”๋กœ ์ดˆ๊ธฐํ™”๋˜๋„๋ก)

- ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๋ชจ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ํ˜น์€ ๋‹ค๋ฅธ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์‹ฑ๊ธ€ํ†ค์„ ์‚ฌ์šฉํ•˜๋“ฏ์ด ์‚ฌ์šฉํ•œ๋‹ค.

 

 

 

 

 

 

 

 

 

 

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ

'BackEnd > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

JAVA DOC ๋ฌธ์„œ ์ƒ์„ฑ  (0) 2021.03.17
BoardManagement - ibatis ์ ์šฉ  (0) 2021.03.16
MAC / ORACLE / DOCKER / OJDBC / Eclipse ์—ฐ๋™  (0) 2021.03.14
์ œ๋„ˆ๋ฆญ ์ดํ•ด  (0) 2021.03.09
Enum(์—ด๊ฑฐํ˜•)  (0) 2021.03.08