728x90
맀κ°λ³μ μ λ¬
this, target, args, @target, @within, @annotation, @args
μμ ν¬μΈνΈμ»· μ§μμλ₯Ό μ¬μ©νλ€λ©΄ μ΄λλ°μ΄μ€μ 맀κ°λ³μλ₯Ό μ λ¬ν μ μμ΅λλ€.
λ€μκ³Ό κ°μ ννλ‘ μ¬μ©ν©λλ€.
@Before("allMember() && args(argName, ..)")
fun log(argName: String) {
println("[log] argName = {${argName}}")
}
ν¬μΈνΈμ»·μμμ μ΄λ¦κ³Ό 맀κ°λ³μμ μ΄λ¦μ λ§μΆμ΄μΌ ν©λλ€.
μμ μμμμλ argNameμΌλ‘ λ§μΆμμ΅λλ€.
μΆκ°λ‘ νμ μ΄ λ©μλμ μ§μ λ νμ μΌλ‘ μ§μ λ©λλ€.
μ¦ args(argName, ..)λ λ€μκ³Ό κ°μ΄ μ μλλ κ²μ λλ€.
args(String, ..)
μμ μ½λ
@Target(CLASS)//ν΄λμ€μ λΆμ
@Retention(RUNTIME)
annotation class ClassAop()
@Target(FUNCTION)
@Retention(RUNTIME)
annotation class MethodAop(
val value: String
)
interface MemberService {
fun hello(param: String): String
}
package hello.advanced.aop.member
import hello.advanced.aop.member.anno.ClassAop
import hello.advanced.aop.member.anno.MethodAop
import org.springframework.stereotype.Component
@ClassAop
@Component
class MemberServiceImpl : MemberService{
@MethodAop("test value")
override fun hello(param: String): String {
return "ok"
}
fun internal(param: String): String {
return "ok"
}
}
package hello.advanced.aop.member
import hello.advanced.aop.member.anno.MethodAop
interface MemberService {
@MethodAop("test value")
fun hello(param: String): String
}
package hello.advanced.aop.member
import org.springframework.stereotype.Component
@Component
class MemberServiceImpl : MemberService{
override fun hello(param: String): String {
return "ok"
}
fun internal(param: String): String {
return "ok"
}
}
package hello.advanced.pointcut
import hello.advanced.aop.member.MemberService
import hello.advanced.aop.member.anno.ClassAop
import hello.advanced.aop.member.anno.MethodAop
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.stereotype.Component
@SpringBootTest
class ParamTest(
@Autowired val memberService: MemberService
) {
@Test
fun success() {
println("memberService Proxy=${memberService.javaClass}")
memberService.hello("helloA")
}
}
@Aspect
@Component
class ParameterAspect {
@Pointcut("execution(* hello.advanced.aop.member..*.*(..))")
fun allMember() {}
@Around("allMember()")
fun logArgs1(joinPoint: ProceedingJoinPoint): Any? {
val arg = joinPoint.args[0]
println("[logArgs1]${joinPoint.signature} ${arg}")
return joinPoint.proceed()
}
@Around("allMember() && args(argName, ..) ")
fun logArgs2(joinPoint: ProceedingJoinPoint, argName: Any): Any? {
println("[logArgs2]${joinPoint.signature} ${argName}")
return joinPoint.proceed()
}
@Before("allMember() && args(arg, ..) ")
fun logArgs3(arg: Any) {
println("[logArgs3] ${arg}")
}
@Before("allMember() && this(obj) ")
fun thisArgs(joinPoint: JoinPoint, obj: MemberService) {//λμ ν΄λμ€ νμ
μ§μ μ§μ κ°λ₯
//== thisλ μ€νλ§μ λ±λ‘λ νλ‘μ κ°μ²΄κ° μΆλ ₯ ==//
println("[this] ${joinPoint.signature}, obj = ${obj.javaClass}")
}
@Before("allMember() && target(obj) ")
fun targetArgs(joinPoint: JoinPoint, obj: MemberService) {//λμ ν΄λμ€ νμ
μ§μ μ§μ κ°λ₯
//== targetμ μ€μ λμ κ°μ²΄κ° μΆλ ₯ ==//
println("[target] ${joinPoint.signature}, obj = ${obj.javaClass}")
}
@Before("allMember() && @target(annotation) ")
fun atTarget(joinPoint: JoinPoint, annotation: ClassAop) {
println("[@target] ${joinPoint.signature}, obj = ${annotation}")
}
@Before("allMember() && @within(annotation) ")
fun atWithin(joinPoint: JoinPoint, annotation: ClassAop) {
println("[@within] ${joinPoint.signature}, obj = ${annotation}")
}
@Before("allMember() && @annotation(annotation) ")
fun atAnnotation(joinPoint: JoinPoint, annotation: MethodAop) {
//== μ λ
Έν
μ΄μ
μ μμ±κ°λ μΆμΆ κ°λ₯ ==//
println("[@annotation] ${joinPoint.signature}, obj = ${annotation}, annotationValue = ${annotation.value}")
}
}
Reference
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-pointcuts
728x90
'ποΈ Spring > AOP' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[AOP] μ΄λλ°μ΄μ€μ μ’ λ₯ (0) | 2022.08.03 |
---|---|
[AOP] AOP ν¬μΈνΈμ»· ννμ (10) - target (0) | 2022.08.02 |
[AOP] AOP ν¬μΈνΈμ»· ννμ (9) - this (0) | 2022.08.02 |
[AOP] AOP ν¬μΈνΈμ»· ννμ (8) - bean (0) | 2022.08.02 |
[AOP] AOP ν¬μΈνΈμ»· ννμ (7) - @args (0) | 2022.08.02 |