public class HelloService { public void sayHello(String name) { System.out.println("Hello, " + name); } } @Interceptor public class LoggingInterceptor { @AroundInvoke public Object logMethod(InvocationContext context) throws Exception { System.out.println("Before calling method: " + context.getMethod().getName()); Object result = context.proceed(); System.out.println("After calling method: " + context.getMethod().getName()); return result; } } <?xml version="1.0" encoding="UTF-8"?> <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" bean-discovery-mode="all"> <interceptors> <class>com.example.LoggingInterceptor</class> </interceptors> </beans> @InterceptorBinding @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface Loggable { } @Loggable public class HelloService { public void sayHello(String name) { System.out.println("Hello, " + name); } }


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