Giriş
Şu satırı dahil ederiz
import jakarta.interceptor.Interceptor;
Örnek
Şöyle yaparız. @Timed olarak işaretli kodlara AOP uygulanıyor
beans.xml'de şöyle yaparız@ApplicationScopedpublic class GreetingService {@Timedpublic String getGreetingTemplate(String language) {//}}@InterceptorBinding@Retention(RetentionPolicy.RUNTIME)@Target({METHOD, TYPE})public @interface Timed {}@Interceptor@Timedpublic class TimedInterceptor {@AroundInvokepublic Object timeInvocation(InvocationContext ctx) throws Exception {// Before methodlong start = System.currentTimeMillis();Object result = ctx.proceed();// after methodreturn result.toString()+" calculated in "+(System.currentTimeMillis()- start) + "ms";}}
<beans>
<interceptors>
<class>fish.payara.jakarta.ee9.start.TimedInterceptor</class>
</interceptors>
</beans>Şöyle yaparız
@Timed
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
public class TimedInterceptor {
@AroundInvoke
public Object auditMethod(InvocationContext ctx) throws Exception {
...
}
}
Hiç yorum yok:
Yorum Gönder