๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปStudy/Spring

์ˆœ์ˆ˜ ์ž๋ฐ”๋กœ AOP ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

์‚ฌ์šฉ์ž์˜ ์š”๊ตฌ์— ๋”ฐ๋ผ kor, eng, math, com ๋„ค ๊ณผ๋ชฉ์˜ ์ด์ ์„ ๊ณ„์‚ฐํ•ด ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ณด์ž. ํ”„๋กœ๊ทธ๋žจ์ด ๋„ˆ๋ฌด ๋А๋ฆฌ๋‹ค๋Š” ํ”ผ๋“œ๋ฐฑ์ด ์žˆ์„ ๋•Œ ๊ฐœ๋ฐœ์ž๋“ค์€ ์‹คํ–‰์— ์‹œ๊ฐ„์ด ์–ผ๋งˆ๋‚˜ ๊ฑธ๋ฆฌ๋Š”์ง€ ์•Œ ํ•„์š”๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๊ณ , ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด ๊ณ„์‚ฐํ•˜๋Š” ์†Œ์Šค๋ฅผ ๋ผ์›Œ๋„ฃ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด๋Š” ์‚ฌ์šฉ์ž์˜ ์š”๊ตฌ์‚ฌํ•ญ๊ณผ๋Š” ๋ณ„๊ฐœ๋กœ ๊ฐœ๋ฐœ์ž๋“ค์ด ํ•„์š”์— ์˜ํ•ด ์‚ฝ์ž…ํ•œ ๋ถ€๋ถ„์ด๋‹ค. (=>๋ณด์กฐ ์—…๋ฌด)

 

 

** ์†Œ์Šค ๊ตฌ์„ฑ

Exam์€ NewlecExam์˜ ์ธํ„ฐํŽ˜์ด์Šค

 

 

Program.java

public class Program {
	public static void main(String[] args) {

		Exam exam = new NewlecExam(1,1,1,1);
		System.out.printf("total is %d\n", exam.total());
			
	}
}

 

NewlecExam.java

public class NewlecExam implements Exam {
	
	private int kor;
	private int eng;
	private int math;
	private int com;
	
	public NewlecExam() {
	}

	public NewlecExam(int kor, int eng, int math, int com) {
		this.kor = kor;
		this.eng = eng;
		this.math = math;
		this.com = com;
	}


	@Override
	public int total() {
		//////////////๋ณด์กฐ ์—…๋ฌด//////////////
		long start = System.currentTimeMillis();
		//////////////๋ณด์กฐ ์—…๋ฌด//////////////
		
		
		////////์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•œ ์—…๋ฌด(์ฃผ ์—…๋ฌด)////////
		int result = kor + eng + math + com;
		////////์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•œ ์—…๋ฌด(์ฃผ ์—…๋ฌด)////////
		
		
		//////////////๋ณด์กฐ ์—…๋ฌด//////////////
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		long end = System.currentTimeMillis();
		String message = (end - start) + "์‹œ๊ฐ„์ด ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.";
		System.out.println(message);
		//////////////๋ณด์กฐ ์—…๋ฌด//////////////
		
		return result;
	}
}

 

์ดํ•ฉ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” total() ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ ์‹คํ–‰ ์†๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๊ปด๋†“์•˜๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•ด๋‹น ๊ณ๋‹ค๋ฆฌ ์ฝ”๋“œ, ๋ณด์กฐ ์—…๋ฌด๋ฅผ ์ง€์šฐ๊ฑฐ๋‚˜ ์‚ฝ์ž…ํ•  ๋•Œ ๋ชจ๋“  ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ ์ฃผ์„ ์ฒ˜๋ฆฌ๋ฅผ ํ–ˆ๋‹ค๊ฐ€ ์ง€์šฐ๋Š” ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฒˆ๊ฑฐ๋กญ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ‰๊ท ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” avg() ํ•จ์ˆ˜ ์•ˆ์—์„œ๋„ ์ด๋Ÿฐ ์ฒ˜๋ฆฌ๋ฅผ ์ผ์ผ์ด ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฒˆ๊ฑฐ๋กœ์›€์„ ์ค„์ด๊ธฐ ์œ„ํ•ด AOP๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ž๋ฐ”์—์„œ๋Š” AOP๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด proxy๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

 

์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ์ฒ˜๋Ÿผ ์ˆ˜์ •ํ•˜์—ฌ AOP ๊ตฌํ˜„์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

 

Program.java

public class Program {
	public static void main(String[] args) {

		Exam exam = new NewlecExam(1,1,1,1);
		
		Exam proxy = (Exam) Proxy.newProxyInstance(
				NewlecExam.class.getClassLoader(), 
				new Class[] {Exam.class},
				new InvocationHandler() {
					
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						long start = System.currentTimeMillis();
						
						Object result = method.invoke(exam, args);
						
						try {
							Thread.sleep(200);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						
						long end = System.currentTimeMillis();
						String message = (end - start) + "์‹œ๊ฐ„์ด ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.";
						System.out.println(message);
						return result;
					}
				}
				);
		//proxy๋Š” ๊ฐ€์งœ exam์ด๋‹ค. 
		//ํ•˜์ง€๋งŒ exam ๊ฐ์ฒด์™€ ๋‹ฎ์•„์žˆ๊ธฐ์— ์‚ฌ์šฉ์ž๊ฐ€ ๋А๋ผ๊ธฐ์—๋Š” exam์„ ์“ฐ๋Š” ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์ด ๋А๊ปด์ง„๋‹ค.
				
		System.out.printf("total is %d\n", proxy.total());
		System.out.printf("avg is %d\n", proxy.total());
	}
}

 

NewlecExam.java

public class NewlecExam implements Exam {
	
	private int kor;
	private int eng;
	private int math;
	private int com;
	
	public NewlecExam() {
	}

	public NewlecExam(int kor, int eng, int math, int com) {
		this.kor = kor;
		this.eng = eng;
		this.math = math;
		this.com = com;
	}

	@Override
	public int total() {
		////////์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•œ ์—…๋ฌด(์ฃผ ์—…๋ฌด)////////
		int result = kor + eng + math + com;
		////////์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•œ ์—…๋ฌด(์ฃผ ์—…๋ฌด)////////
		
		return result;
	}

	@Override
	public float avg() {
   		////////์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•œ ์—…๋ฌด(์ฃผ ์—…๋ฌด)////////
		float result = total() / 4.0f;
		////////์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•œ ์—…๋ฌด(์ฃผ ์—…๋ฌด)////////

		return result;
	}

 

์ถœ๋ ฅ

 

์ฃผ ์—…๋ฌด ์•ž ๋’ค์— ์ง์ ‘ ์จ์žˆ๋˜ ๋ณด์กฐ ์—…๋ฌด๋ฅผ ๋นผ๊ณ  proxy๋กœ ์ฒ˜๋ฆฌํ•˜์˜€๋‹ค. ๋งŒ์•ฝ ๋ณด์กฐ ์—…๋ฌด๊ฐ€ ํ•„์š”๊ฐ€ ์—†์–ด์ง€๋ฉด ์†Œ์Šค ์ฝ”๋“œ๋ฅผ proxy.total()์—์„œ exam.total()์œผ๋กœ ์ˆ˜์ •ํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์ด์ „๋ณด๋‹ค ํ›จ์”ฌ ๊ฐ„ํŽธํ•ด์กŒ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ์ž๋ฐ”๋กœ๋งŒ AOP ๊ตฌํ˜„์„ ํ•˜๋Š” ๊ฒƒ์—๋Š” ์—ฌ์ „ํžˆ ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์Šคํ”„๋ง์„ ์ด์šฉํ•˜์—ฌ ๋ณด๋‹ค ํŽธ๋ฆฌํ•˜๊ฒŒ AOP ๊ตฌํ˜„์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

 


๋‰ด๋ ‰์ฒ˜ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์ •๋ฆฌํ•œ ๊ฒŒ์‹œ๊ธ€์ž…๋‹ˆ๋‹ค.

www.youtube.com/user/newlec1