AOP
μ²μλΆν° AOP μ΄λ‘ λ° μ©μ΄λ₯Ό νμ΅νκΈ°μ μ΄λ €μ°λ κ°λ¨ν μμ λ₯Ό ν΅ν΄ νμ΅ν ν μμλ³Ό κ²μ΄λ€.
1. AOPκ° νμν μν©
1) μμ
- λͺ¨λ λ©μλμ νΈμΆ μκ°μ μΈ‘μ νκ³ μΆλ€λ©΄?
- λͺ¨λ λ©μλμ μμκ³Ό λμ μκ° μΈ‘μ λ‘μ§μ μΆκ°νμλ€.
- λ§μ½, κ°μκΈ° μκ° λ¨μλ₯Ό λ³κ²½ν΄μΌ νλ€λ©΄ λͺ¨λ λ©μλλ₯Ό λ€μ μμ ν΄μΌ νλ€.
package hello.hello.spring.service;
import hello.hello.spring.domain.Member;
import hello.hello.spring.repository.MemberRepository;
import hello.hello.spring.repository.MemoryMemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Transactional
public class MemberService {
private final MemberRepository memberRepository;
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 end = System.currentTimeMillis();
long timeMs = end - start;
System.out.println("join = " + timeMs + "ms");
}
}
private void validateDuplicateMember(Member member) {
memberRepository.findByName(member.getName())
.ifPresent(member1 -> {
throw new IllegalStateException("μ΄λ―Έ μ‘΄μ¬νλ νμμ
λλ€.");
});
}
// μ 체 νμ μ‘°ν
public List<Member> findMembers() {
long start = System.currentTimeMillis();
try {
return memberRepository.findAll();
} finally {
long end = System.currentTimeMillis();
long timeMs = end - start;
System.out.println("findMembers = " + timeMs + "ms");
}
}
// νμ μ‘°ν
public Optional<Member> findOne(Long memberId) {
long start = System.currentTimeMillis();
try {
return memberRepository.findById(memberId);
} finally {
long end = System.currentTimeMillis();
long timeMs = end - start;
System.out.println("findOne = " + timeMs + "ms");
}
}
}
2) μμ κ²°κ³Ό
- νμ κ°μ λ° μ‘°ν ν μ€νΈλ₯Ό ν λλ§λ€ μκ°μ΄ μΈ‘μ λλ©° ν΄λΉ λ‘κ·Έλ₯Ό λ¨κΈ°κ³ μλ€.
- μ²μμΌλ‘ κ°μ λ° μ‘°ν ν μ€νΈν λ μκ°μ΄ μ’ λ μ€λ κ±Έλ¦°λ€.
3) λ¬Έμ μ
- ν΅μ¬ κ΄μ¬ μ¬νμ λΉμ¦λμ€ λ‘μ§μ΄κ³ μκ°μ μΈ‘μ νλ κΈ°λ₯μ κ³΅ν΅ κ΄μ¬ μ¬νμ΄λ€.
- μκ°μ μΈ‘μ νλ λ‘μ§κ³Ό ν΅μ¬ λΉμ¦λμ€μ λ‘μ§μ΄ μμ¬μ μ μ§λ³΄μκ° μ΄λ ΅λ€.
- μκ°μ μΈ‘μ νλ λ‘μ§μ λ³λμ κ³΅ν΅ λ‘μ§μΌλ‘ λ§λ€κΈ° λ§€μ° μ΄λ ΅λ€.
- μκ°μ μΈ‘μ νλ λ‘μ§μ λ³κ²½ν λ λͺ¨λ λ‘μ§μ μ°Ύμκ°λ©΄μ λ³κ²½ν΄μΌ νλ€.