import javax.interceptor.AroundInvoke; import javax.interceptor.Interceptor; import javax.interceptor.InvocationContext; @Interceptor public class LoggingInterceptor { @AroundInvoke public Object logMethod(InvocationContext context) throws Exception { System.out.println("Before method execution: " + context.getMethod().getName()); try { Object result = context.proceed(); System.out.println("After method execution: " + context.getMethod().getName()); return result; } catch (Exception e) { System.out.println("Exception thrown: " + e.getMessage()); throw e; } } } public class UserService { @LoggingInterceptor public void createUser(String username) { System.out.println("Creating user: " + username); } } <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_1.xsd"> <interceptors> <class>com.example.LoggingInterceptor</class> </interceptors> </beans>


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