public class LoggingAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Method " + methodInvocation.getMethod().getName() + " is about to be called.");
Object result = methodInvocation.proceed();
System.out.println("Method " + methodInvocation.getMethod().getName() + " has been called.");
return result;
}
}
public class ExampleService {
public void doSomething() {
System.out.println("Doing something...");
}
}
public class Main {
public static void main(String[] args) {
LoggingAspect loggingAspect = new LoggingAspect();
ExampleService exampleService = new ExampleService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(exampleService);
proxyFactory.addAdvice(loggingAspect);
ExampleService proxy = (ExampleService) proxyFactory.getProxy();
proxy.doSomething();
}
}