Я использую Spring AOP, ниже указан класс обслуживания и аспект.
Я хочу вызвать метод assignValues () раньше, когда вызывается multiply ().
package com.test.service;
import org.springframework.stereotype.Service;
@Service
public class MathService {
int a,b;
public void assignValues() {
a=3;
b=10;
}
public Integer multiply(){
System.out.println("--multiply called---");
int res = a*b;
System.out.println("values :: " + a+ "*" + b +"= " + res);
return res;
}
public Integer multiply(int a, int b){
int res = a*b;
System.out.println(a+ "*" + b +"= " + res);
return res;
}
}
Класс аспектов ниже:
@Component
@Aspect
public class TimeLoggingAspect {
@Around("execution(* com.test.service.*.*(..))")
public void userAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("@Around: Before calculation-"+ new Date());
joinPoint.proceed();
System.out.println("@Around: After calculation-"+ new Date());
}
@Before("execution(* com.test.service.assignValues(..))")
public void logBeforeV1(JoinPoint joinPoint)
{
System.out.println("-before- : " + joinPoint.getSignature().getName());
}
}
Тестовый класс:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringAOPTest {
public static void main(String[] args) {
//...
MathService mathService = ctx.getBean(MathService.class);
mathService.multiply();//i want to call assignValues() method to assign the values of a and b through AOP, before method multiply() is called
}
}
Как вызывать метод assignValues () с использованием аспекта @ До всякий раз, когда вызывается multiply ().