절차지향 프로그래밍(Procedural Programming)
물이 위에서 아래로 흐르는것처럼 순차적인 처리가 중요하고 프로그램 전체가 유기적으로 연결되도록 만드는 기법
유지보수가 어렵다. 디버깅이 어렵다.
실행순서가 정해져 있으므로 코드 순서가 바뀌면 동일한 결과를 보장할 수 없음
객체지향 프로그래밍(Object Oriented Programming)
실제 세계를 모델링하여 소프트웨어를 개발하는 방법이다.
데이터와 절차를 하나의 덩어리로 묶어서 생각하게 된다. 마치 컴퓨터 부품을 하나씩 사다가 컴퓨터를 조립하는 것과 같다.
상속,다형성,캡슐화
관점 지향 프로그래밍(Aspect Oriented Programming)
AOP가 필요한 상황이 어디일까?
- 모든 메소드의 호출 시간을 측정하고 싶다면?
-공통 관심 사항(cross-cutting concern) vs 핵심 관심 사항(core concern)
-회원 가입 시간, 회원 조회 시간을 측정하고싶다.
@RequiredArgsConstructor
@Service
public class PersonService {
private final PersonRepository personRepository;
@Transactional
public Long updatePersonService(Long id, PersonRequestDto personRequestDto){
long start = System.currentTimeMillis();
try {
Optional<Person> person = personRepository.findById(id);
person.update(personRequestDto);
return id;
}
finally {
long finish = System.currentTimeMillis();
long timeMs = finish - start;
System.out.println("updateperson" + timeMs + "ms");
}
문제
-하나하나의 로직마다 측정 시간 로직을 추가해야한다.
-시간을 측정하는 기능은 핵심 관심 사항이 아니다.
-시간을 측정하는 로직은 공통 관심 사항이다.
-시간을 측정하는 로직과 핵심 비즈니스의 로직이 섞여서 유지보수가 어렵다.
-시간을 측정하는 로직을 별도의 공통 로직으로 만들기 매우 어렵다.
-시간을 측정하는 로직을 변경할 때 모든 로직을 찾아가면서 변경해야 한다.
@Component
@Aspect //AOP 적용 annotation
public class TimeTraceAop {
@Around("execution(*tanibourne.pracspring..*(..))") //타겟팅( )
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");
}
}
}
'Framework > Spring' 카테고리의 다른 글
@BUILDER 란? 써야하는 이유는 무엇일까? (0) | 2022.08.03 |
---|---|
💡 개발자 테스트 코드 작성 시 장/단점과 테스트 종류 별 (단위 테스트, 통합 테스트, E2E 테스트) 로 특징은? (0) | 2022.08.01 |