๐Ÿ–ฅ๏ธ Web Development Study/Back-End

[Back-end] ์Šคํ”„๋ง ์ž…๋ฌธ :: AOP

ibelieveinme 2025. 1. 29. 23:05
728x90

*AOP(Aspect Oriented Programming)

: ๊ณตํ†ต ๊ด€์‹ฌ ์‚ฌํ•ญ(cross-cutting concern)๊ณผ ํ•ต์‹ฌ ๊ด€์‹ฌ ์‚ฌํ•ญ(core concern)์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ.

 

๋ชจ๋“  ๋ฉ”์†Œ๋“œ์˜ ํ˜ธ์ถœ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ

ํšŒ์› ๊ฐ€์ž… ์‹œ๊ฐ„, ํšŒ์› ์กฐํšŒ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ


1. ํšŒ์› ๊ฐ€์ž…, ํšŒ์› ์กฐํšŒ ์‹œ๊ฐ„ ์ธก์ •ํ•˜๊ธฐ(์ผ์ผ์ด)

import com.yoonsung.firstproject.domain.Member;
import com.yoonsung.firstproject.repository.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class MemberService {

    private final MemberRepository memberRepository;

    @Autowired
    public MemberService(MemberRepository memberRepository){
        this.memberRepository = memberRepository;
    }

    // ํšŒ์›๊ฐ€์ž…
    public Long join(Member member){

        long start = System.currentTimeMillis();

        try {
            validateDuplicateMember(member); // ์ค‘๋ณต ํšŒ์› ๊ฒ€์ฆ
            memberRepository.save(member);
            return member.getId();
        }
        finally{
            long finish = System.currentTimeMillis();
            long timeMs = finish - start;
            System.out.print("join Time: " + timeMs + "ms");
        }
    }

    ...
    
    /**
     * ์ „์ฒด ํšŒ์› ์กฐํšŒ
     */
    public List<Member> findMembers(){
        long start = System.currentTimeMillis();

        try {
            return memberRepository.findAll();
        }
        finally{
            long finish = System.currentTimeMillis();
            long timeMs = finish - start;
            System.out.print("findId Time: " + timeMs + "ms");
        }
    }

   ...
}

ํ•จ์ˆ˜ ํ•˜๋‚˜ํ•˜๋‚˜์— try finally ๋ฌธ์œผ๋กœ ์‹œ๊ฐ„ ์ธก์ •ํ•˜๊ธฐ.

ํ•ต์‹ฌ์ฝ”๋“œ์™€ ๊ณตํ†ต ์ฝ”๋“œ๊ฐ€ ์„ž์ด๊ณ  ์ค‘๋ณต ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ๋ฐœ์ƒํ•œ๋‹ค.

 

 

2. ๊ฐœ์„ :: AOP ์‚ฌ์šฉํ•˜๊ธฐ

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class TimeTraceAop {

    @Around("execution(* hello.hellospring..*(..))")
    public Object execute(ProceedingJoinPoint joinPoint) throws Throwable{
        long start = System.currentTimeMillis();
        System.out.println("START: " + joinPoint.toString());
        try{
          return joinPoint.proceed();
        } finally{
            long finish = System.currentTimeMillis();
            long timeMs = finish - start;
            System.out.println("END: " + joinPoint.toString() + " " + timeMs + "ms");
        }
    }
}

๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค jointPoint ๊ฐ€ intercept ํ•ด์„œ ํ˜ธ์ถœ๋œ๋‹ค.

Spring bean ์— ๋“ฑ๋กํ•ด์ฃผ๊ฑฐ๋‚˜ Component ์Šค์บ”์œผ๋กœ ๋งŒ๋“ค๊ธฐ. Spring bean์ด ๋” ๊ฐ€์‹œ์ ์ธ ํ‘œํ˜„์ด๋ผ ์„ ํ˜ธ๋œ๋‹ค.

 

 

 

728x90