import javax.interceptor.AroundInvoke; import javax.interceptor.Interceptor; import javax.interceptor.InvocationContext; @Interceptor public class LoggingInterceptor { @AroundInvoke public Object logMethod(InvocationContext invocationContext) throws Exception { System.out.println("Method name: " + invocationContext.getMethod().getName()); Object result = invocationContext.proceed(); System.out.println("Method executed successfully."); return result; } } @Logged public class UserService { @Interceptors(LoggingInterceptor.class) public void createUser(String username, String password) { } } @Interceptor @Priority(Interceptor.Priority.APPLICATION) public class TransactionInterceptor { } @Interceptor @Priority(Interceptor.Priority.LIBRARY_BEFORE) public class LoggingInterceptor { } @InterceptorBinding @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface Logged { } @Logged public class UserService { public void createUser(String username, String password) { } }


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