Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. package com.test.aop;
  2.  
  3. import org.aspectj.lang.JoinPoint;
  4. import org.aspectj.lang.annotation.Aspect;
  5. import org.aspectj.lang.annotation.Before;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.aop.support.AopUtils;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.context.annotation.EnableAspectJAutoProxy;
  13. import org.springframework.core.annotation.Order;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.test.context.ContextConfiguration;
  16. import org.springframework.test.context.junit4.SpringRunner;
  17. import org.springframework.test.context.support.AnnotationConfigContextLoader;
  18.  
  19. @RunWith(SpringRunner.class)
  20. @ContextConfiguration(loader= AnnotationConfigContextLoader.class)
  21. public class AspectTest {
  22. @Autowired Runnable main;
  23.  
  24. @Test
  25. public void checkOrder() throws Exception {
  26. System.out.println(AopUtils.isAopProxy(main));
  27. main.run();
  28. }
  29.  
  30. @Configuration
  31. @EnableAspectJAutoProxy
  32. static public class TestContext {
  33.  
  34. @Bean
  35. public Runnable main() {
  36. return new Runnable() {
  37. public void run() {
  38. System.out.println("target executed");
  39. }
  40. };
  41. }
  42.  
  43. @Aspect
  44. @Order(-1)
  45. @Component
  46. public static class OrderMinus1Aspect {
  47. @Before("execution(void *.run())")
  48. public void printOrder(JoinPoint jp) {
  49. System.out.println("order -1");
  50. }
  51. }
  52.  
  53. @Aspect
  54. @Order(0)
  55. @Component
  56. public static class Order0Aspect {
  57. @Before("execution(void *.run())")
  58. public void printOrder(JoinPoint jp) {
  59. System.out.println("order 0");
  60. }
  61. }
  62. @Aspect
  63. @Order(1)
  64. @Component
  65. public static class Order1Aspect {
  66. @Before("execution(void *.run())")
  67. public void printOrder(JoinPoint jp) {
  68. System.out.println("order 1");
  69. }
  70. }
  71. @Aspect
  72. @Order(2)
  73. @Component
  74. public static class Order2Aspect {
  75. @Before("execution(void *.run())")
  76. public void printOrder(JoinPoint jp) {
  77. System.out.println("order 2");
  78. }
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement