[Spring] 7.AOP
1. AOP๊ฐ ํ์ํ ์ํฉ
ex)
- ๋ชจ๋ ๋ฉ์๋์ ํธ์ถ ์๊ฐ ์ธก์ ํ๊ณ ์ถ๋ค๋ฉด?
package hello.hellospring.service;
@Transactional
public class MemberService {
/**
* ํ์๊ฐ์
*/
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.println("join " + 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.println("findMembers " + timeMs + "ms");
}
}
}
- ์๊ฐ ์ธก์ ๋ก์ง์ ํต์ฌ ๋ก์ง์ด ์๋๋ผ ๊ณตํต ๋ก์ง
- ํต์ฌ ๊ด์ฌ ์ฌํญ vs ๊ณตํต ๊ด์ฌ ์ฌํญ
- ์๊ฐ์ ์ธก์ ํ๋ ๋ก์ง๊ณผ ํต์ฌ ๋น์ฆ๋์ค์ ๋ก์ง์ด ์์ฌ์ ์ ์ง๋ณด์๊ฐ ์ด๋ ต๋ค.
2. AOP ์ ์ฉ
- AOP: Aspect Oriented Programming
- ๊ณตํต ๊ด์ฌ ์ฌํญ(cross-cutting concern) vs ํต์ฌ ๊ด์ฌ ์ฌํญ(core concern) ๋ถ๋ฆฌ
์๊ฐ ์ธก์ AOP ๋ฑ๋ก
ใด hello.hellospring - aop ํด๋์ ์์ฑ
@Component
@Aspect
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 +
}
}
- ํด๋น ๊ฐ์ฒด ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก
- @Component ํด๋ ๋๊ณ Configuration ์ ์คํ๋ง ๋น ๋ฑ๋ก๋ ๊ฐ๋ฅ
- AOP๋ ์ธ์งํ ์ ์๋๋ก Config์์ ๊ด๋ฆฌํ๋ ๊ฒ์ด ์ข์ (์ ํํํด์ ์ฐ๋๊ฒ ์๋๊ธฐ ๋๋ฌธ์)
์ฌ๊ธฐ์๋ ๊ฑ @Component
- @Around : ("execution(*hello.hellospring..*(..))")
ํจํค์ง๋ช .ํด๋.ํด๋์ค๋ช (ํ๋ผ๋ฏธํฐํ์ )
= ํจํค์ง ํ์ ๋ค ์ ์ฉ
-> ํต์ฌ๊ด์ฌ์ฌํญ ๊น๋ ์ ์ง ๊ฐ๋ฅ
-> ๋ณ๊ฒฝ์ด ํ์ํ๋ฉด ํด๋น ๋ก์ง๋ง ๋ณ๊ฒฝ ๊ฐ๋ฅ
[์คํ๋ง์ AOP ๋์ ๋ฐฉ์ ์ค๋ช ]
AOP ์ ์ฉ ์ ์์กด๊ด๊ณ
AOP ์ ์ฉ ํ ์์กด๊ด๊ณ
- ๊ฐ์ง ๋ฉค๋ฒ์๋น์ค(ํ๋ก์) ๋ง๋ฆ
- ์คํ๋ง ๋น ๋ฑ๋กํ ๋ ๊ฐ์ง ์คํ๋ง๋น์ ์ธ์ฐ๊ณ ๊ฐ์ง ์คํ๋ง ๋น์ด ๋๋๋ฉด proceedํ๊ณ ์ง์ง ๋ฉค๋ฒ์๋น์ค ํธ์ถ
AOP ์ ์ฉ ์ ์ ์ฒด๊ทธ๋ฆผ
AOP ์ ์ฉ ํ ์ ์ฒด๊ทธ๋ฆผ
* ์ปจํธ๋กค๋ฌ์ ๋ฉค๋ฒ ์๋น์ค DI ๋ ๋ ํ์ธ๊ฐ๋ฅ
'๐จโ๐ป Web Development > Spring - Intro' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] 6.์คํ๋ง DB ์ ๊ทผ ๊ธฐ์ (0) | 2023.02.14 |
---|---|
[Spring] 5.ํ์ ๊ด๋ฆฌ ์์ - ์น MVC ๊ฐ๋ฐ (0) | 2023.02.13 |
[Spring] 4.์คํ๋ง ๋น๊ณผ ์์กด๊ด๊ณ (0) | 2023.02.11 |
[Spring] 3.ํ์๊ด๋ฆฌ ์์ - ๋ฐฑ์๋ ๊ฐ๋ฐ (0) | 2022.07.03 |
[Spring] 2.์คํ๋ง ์น ๊ฐ๋ฐ ๊ธฐ์ด (1) | 2022.07.01 |
์ต๊ทผ๋๊ธ