λ°±μ—”λ“œ(Back-End) 개발/Java

[JAVA] λ””μžμΈ νŒ¨ν„΄ - ν”„λ‘μ‹œ νŒ¨ν„΄(Proxy)

rabo93 2025. 5. 28. 13:23

ν”„λ‘μ‹œ νŒ¨ν„΄(Proxy)

  • μ–΄λ–€ 객체에 λŒ€ν•œ 접근을 μ œμ–΄ν•˜κΈ° μœ„ν•΄, λŒ€λ¦¬ 객체(proxy)λ₯Ό μ‚¬μš©ν•˜λŠ” νŒ¨ν„΄
  • Spring AOPλŠ” ν”„λ‘μ‹œ νŒ¨ν„΄ 기반으둜 λ™μž‘ν•¨. 핡심 둜직 전후에 λΆ€κ°€κΈ°λŠ₯ μ‚½μž… κ°€λŠ₯
// μ„œλΉ„μŠ€ λ©”μ„œλ“œ μ‹€ν–‰ 전후에 λ‘œκΉ… 예제(AOP)
@Aspect // ν”„λ‘μ‹œ 객체가 λŒ€μƒ 객체의 λ©”μ„œλ“œλ₯Ό κ°μ‹ΈλŠ” ꡬ쑰
@Component
public class LoggingAspect {

	// λͺ¨λ“  Service νŒ¨ν‚€μ§€μ˜ λ©”μ„œλ“œ μ‹€ν–‰ μ „
	@Before("execution(* com.winbit.project1.service.*.*(..))")
	public void beforeLog(JoinPoint joinPoint) {
		System.out.println("[Before]" + joinPoint.getSignature().getName()); //ex: [Before]getBoardList
	}
	
	// μ‹€ν–‰ ν›„
	@After("execution(* com.winbit.project1.service.*.*(..))")
	public void afterLog(JoinPoint joinPoint) {
		System.out.println("[After]" + joinPoint.getSignature().getName()); //ex: [After]getBoardList
	}
}