<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Pointcut("execution(* com.example.app.service.*.*(..))")
public void serviceMethods() {}
@Before("serviceMethods()")
public void logBefore() {
System.out.println("Before method execution.");
}
@After("serviceMethods()")
public void logAfter() {
System.out.println("After method execution.");
}
@Around("serviceMethods()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Before method execution.");
Object result = joinPoint.proceed();
System.out.println("After method execution.");
return result;
}
}
<aop:aspectj-autoproxy />
@Service
public class UserService {
@Loggable
public void createUser(User user) {
}
}
<bean id="userService" class="com.example.app.service.UserService">
<property name="createUserAdvice" ref="createUserAdvice" />
</bean>
<bean id="createUserAdvice" class="com.example.app.advice.CreateUserAdvice" />