• Source
    1. //Ejemplo tomado de https://www.geeksforgeeks.org/aspect-oriented-programming-and-aop-in-spring-framework/
    2.  
    3. package com.aspect
    4.  
    5. import org.aspectj.lang.annotation.Aspect;
    6. import Java.lang.RuntimeException;
    7. import org.springframework.context.ApplicationContext;
    8. import org.springframework.context.support.ClassPathXmlApplicationContext;
    9.  
    10. // Logging class is anotated with @Aspect
    11. // and will contain advices.
    12. @Aspect
    13. class Logging {
    14. }
    15.  
    16. // The class ImplementAspect
    17. // contains method Aspectcall
    18. // and the advices will be applied
    19. // on that method for demo.
    20. public class ImplementAspect {
    21. public static void main(String args[])
    22. {
    23.  
    24. Scanner sc = new Scanner(System.in);
    25. System.out.println("my first aspect");
    26.  
    27. // **Add beanconfiguration file
    28. // in your programme when executing.**
    29. ApplicationContext ctx
    30. = new ClassPathXmlApplicationContext("beanconfigfile.XML");
    31.  
    32. ImplementAspect call
    33. = (ImplementAspect)ctx.getbean("aspect");
    34.  
    35. System.out.println("enter an integer");
    36. int a = sc.nextInt();
    37. if (a == 1) {
    38. throw new RuntimeException(msg);
    39. }
    40. else {
    41. call.aspectCall();
    42. }
    43. call.myMethod();
    44. }
    45.  
    46. public void aspectCall()
    47. {
    48. System.out.println("Applying advices"
    49. + " for the first time");
    50. }
    51.  
    52. public void myMethod()
    53. {
    54. System.out.println("This is an"
    55. + " extra method");
    56. }
    57. }
    58.  
    59. // Program to show types of Advices
    60.  
    61. @Aspect
    62. class Logging {
    63.  
    64. // Implementing all the five pieces of advice
    65. // to execute AfterThrowing advice enter integer value as 1.
    66.  
    67. // **Before**
    68. @Before("execution(public void com.aspect.ImplementAspect.aspectCall())")
    69. public void loggingAdvice1()
    70. {
    71. System.out.println("Before advice is executed");
    72. }
    73.  
    74. // **After**
    75. @After("execution(public void com.aspect.ImplementAspect.aspectCall())")
    76. public void loggingAdvice2()
    77. {
    78. System.out.println("Running After Advice.");
    79. }
    80.  
    81. // **Around**
    82. @Around("execution(public void com.aspect.ImplementAspect.myMethod())")
    83. public void loggingAdvice3()
    84. {
    85. System.out.println("Before and After invoking method myMethod");
    86. }
    87.  
    88. // **AfterThrowing**
    89. @AfterThrowing("execution(" public void com.aspect.ImplementAspect.aspectCall())
    90. ")
    91. public void
    92. loggingAdvice4()
    93. {
    94. System.out.println("Exception thrown in method");
    95. }
    96.  
    97. // **AfterRunning**
    98. @AfterReturning("execution(public void com.aspect.ImplementAspect.myMethod())")
    99. public void loggingAdvice5()
    100. {
    101. System.out.println("AfterReturning advice is run");
    102. }
    103. }
    104.  
    105. @Aspect
    106. class Logging {
    107.  
    108. // Passing a JoinPoint Object
    109. // into parameters of the method
    110. // with the annotated advice
    111. // enables to print the information
    112. /// when the advice is executed
    113. // with the help of toString() method
    114. // present in it.
    115.  
    116. @Before("execution(public void com.aspect.ImplementAspect.aspectCall())")
    117. public void loggingAdvice1(JoinPoint joinpoint)
    118. {
    119. System.out.println("Before advice is executed");
    120. System.out.println(joinpoint.toString());
    121. }
    122. }
    123.  
    124. // Program to shgw PointCuts
    125.  
    126. @Aspect
    127. class Logging {
    128.  
    129. // pointcut() is a dummy method
    130. // required to hold @Pointcut annotation
    131. // pointcut() can be used instead of writing line 1
    132. // whenever required, as done in line 4.
    133. // This prevents a repetition of code.
    134.  
    135. @Pointcut("execution(public void com.aspect.ImplementAspect.aspectCall())") // line 1
    136. public void pointCut()
    137. {
    138. }
    139.  
    140. // pointcut() is used to avoid repeatition of code
    141. @Before("pointcut()")
    142. public void loggingAdvice1()
    143. {
    144. System.out.println("Before advice is executed");
    145. }
    146. }