//Ejemplo tomado de https://www.geeksforgeeks.org/aspect-oriented-programming-and-aop-in-spring-framework/
package com.aspect
import org.aspectj.lang.annotation.Aspect;
import Java.lang.RuntimeException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// Logging class is anotated with @Aspect
// and will contain advices.
@Aspect
class Logging {
}
// The class ImplementAspect
// contains method Aspectcall
// and the advices will be applied
// on that method for demo.
public class ImplementAspect {
public static void main
(String args
[]) {
Scanner sc
= new Scanner
(System.
in); System.
out.
println("my first aspect");
// **Add beanconfiguration file
// in your programme when executing.**
ApplicationContext ctx
= new ClassPathXmlApplicationContext("beanconfigfile.XML");
ImplementAspect call
= (ImplementAspect)ctx.getbean("aspect");
System.
out.
println("enter an integer"); int a = sc.nextInt();
if (a == 1) {
}
else {
call.aspectCall();
}
call.myMethod();
}
public void aspectCall()
{
System.
out.
println("Applying advices" + " for the first time");
}
public void myMethod()
{
System.
out.
println("This is an" + " extra method");
}
}
// Program to show types of Advices
@Aspect
class Logging {
// Implementing all the five pieces of advice
// to execute AfterThrowing advice enter integer value as 1.
// **Before**
@Before("execution(public void com.aspect.ImplementAspect.aspectCall())")
public void loggingAdvice1()
{
System.
out.
println("Before advice is executed"); }
// **After**
@After("execution(public void com.aspect.ImplementAspect.aspectCall())")
public void loggingAdvice2()
{
System.
out.
println("Running After Advice."); }
// **Around**
@Around("execution(public void com.aspect.ImplementAspect.myMethod())")
public void loggingAdvice3()
{
System.
out.
println("Before and After invoking method myMethod"); }
// **AfterThrowing**
@AfterThrowing("execution(" public void com.aspect.ImplementAspect.aspectCall())
")
public void
loggingAdvice4()
{
System.out.println("Exception thrown in method
"); }
// **AfterRunning**
@AfterReturning("execution(public void com.aspect.ImplementAspect.myMethod())")
public void loggingAdvice5()
{
System.out.println("AfterReturning advice is run");
}
}
@Aspect
class Logging {
// Passing a JoinPoint Object
// into parameters of the method
// with the annotated advice
// enables to print the information
/// when the advice is executed
// with the help of toString() method
// present in it.
@Before("execution(public void com.aspect.ImplementAspect.aspectCall())")
public void loggingAdvice1(JoinPoint joinpoint)
{
System.out.println("Before advice is executed");
System.out.println(joinpoint.toString());
}
}
// Program to shgw PointCuts
@Aspect
class Logging {
// pointcut() is a dummy method
// required to hold @Pointcut annotation
// pointcut() can be used instead of writing line 1
// whenever required, as done in line 4.
// This prevents a repetition of code.
@Pointcut("execution(public void com.aspect.ImplementAspect.aspectCall())") // line 1
public void pointCut()
{
}
// pointcut() is used to avoid repeatition of code
@Before("pointcut()")
public void loggingAdvice1()
{
System.out.println("Before advice is executed");
}
}