import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class LoggingInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("Before method: " + methodInvocation.getMethod().getName()); Object result = methodInvocation.proceed(); System.out.println("After method: " + methodInvocation.getMethod().getName()); return result; } } import org.aopalliance.aop.Advice; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import java.lang.reflect.Proxy; public class MainClass { public static void main(String[] args) { MyService myService = new MyServiceImpl(); MethodInterceptor interceptor = new LoggingInterceptor(); MyService proxy = (MyService) Proxy.newProxyInstance( MyService.class.getClassLoader(), new Class[]{MyService.class}, (MethodInvocation methodInvocation) -> { interceptor.invoke(methodInvocation); return methodInvocation.proceed(); } ); proxy.doSomething(); } } interface MyService { void doSomething(); } class MyServiceImpl implements MyService { @Override public void doSomething() { System.out.println("Doing something..."); } } properties # application.properties aopalliance.enabled=true aopalliance.logging.level=WARN


上一篇:
下一篇:
切换中文