Что выведет код?
Задача по основам AOP в Spring, понятиям Joinpoint, Pointcut, Advice, аннотациям @Before, @After, @Around. Сложность легкая.
Подробный разбор через 30 минут!🫡
#TasksSpring
Задача по основам AOP в Spring, понятиям Joinpoint, Pointcut, Advice, аннотациям @Before, @After, @Around. Сложность легкая.
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.ProceedingJoinPoint;
@SpringBootApplication
@EnableAspectJAutoProxy
public class Task041224_2 {
public static void main(String[] args) {
SpringApplication.run(Task041224_2.class, args);
}
@Bean
public CommandLineRunner demo(Service0412 service) {
return args -> {
service.performTask();
};
}
}
@Component
class Service0412 {
public void performTask() {
System.out.println("Executing performTask...");
}
}
@Aspect
@Component
class LoggingAspect0412 {
@Before("execution(* Service0412.performTask(..))")
public void logBefore() {
System.out.println("Before advice");
}
@After("execution(* Service0412.performTask(..))")
public void logAfter() {
System.out.println("After advice");
}
@Around("execution(* Service0412.performTask(..))")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Around: Before");
Object result = joinPoint.proceed();
System.out.println("Around: After");
return result;
}
}
#TasksSpring