Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
spring aop order 테스트
package com.test.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringRunner.class)
@ContextConfiguration(loader= AnnotationConfigContextLoader.class)
public class AspectTest {
@Autowired Runnable main;
@Test
public void checkOrder() throws Exception {
System.out.println(AopUtils.isAopProxy(main));
main.run();
}
@Configuration
@EnableAspectJAutoProxy
static public class TestContext {
@Bean
public Runnable main() {
return new Runnable() {
public void run() {
System.out.println("target executed");
}
};
}
@Aspect
@Order(-1)
@Component
public static class OrderMinus1Aspect {
@Before("execution(void *.run())")
public void printOrder(JoinPoint jp) {
System.out.println("order -1");
}
}
@Aspect
@Order(0)
@Component
public static class Order0Aspect {
@Before("execution(void *.run())")
public void printOrder(JoinPoint jp) {
System.out.println("order 0");
}
}
@Aspect
@Order(1)
@Component
public static class Order1Aspect {
@Before("execution(void *.run())")
public void printOrder(JoinPoint jp) {
System.out.println("order 1");
}
}
@Aspect
@Order(2)
@Component
public static class Order2Aspect {
@Before("execution(void *.run())")
public void printOrder(JoinPoint jp) {
System.out.println("order 2");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.