我严格按照这里的教程(https://quarkus.io/guides/cdi#interceptors),并根据这个迷你示例(https://github.com/quarkusio/quarkus-quickstarts/tree/main/getting-started)编写一个简单的拦截器
我的拦截器:
package org.acme.getting.started;
import javax.annotation.Priority;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@Logged
@Priority(2020)
@Interceptor
public class InvocationLogger {
@AroundInvoke
Object logInvocation(InvocationContext context) {
System.out.println("Entering method:" + context.getMethod().getName());
try {
Object ret = context.proceed();
return ret;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
装订注释:
package org.acme.getting.started;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;;
@Retention(value = RUNTIME)
@Target(value = { METHOD, TYPE })
public @interface Logged {
}
我尝试绑定的服务:
package org.acme.getting.started;
import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class GreetingService {
@Logged
public String greeting(String name) {
return "hello " + name;
}
}
但是我得到了Interceptor没有绑定
错误:
Caused by: javax.enterprise.inject.spi.DefinitionException: Interceptor has no bindings: org.acme.getting.started.InvocationLogger
at io.quarkus.arc.processor.Interceptors.createInterceptor(Interceptors.java:44)
at io.quarkus.arc.processor.BeanDeployment.findInterceptors(BeanDeployment.java:1112)
at io.quarkus.arc.processor.BeanDeployment.registerBeans(BeanDeployment.java:233)
at io.quarkus.arc.processor.BeanProcessor.registerBeans(BeanProcessor.java:115)
at io.quarkus.arc.deployment.ArcProcessor.registerBeans(ArcProcessor.java:376)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:920)
at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2415)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
at java.base/java.lang.Thread.run(Thread.java:829)
at org.jboss.threads.JBossThread.run(JBossThread.java:501)
我做错什么了吗?
缺少的部分是将@InterceptorBind
注释添加到public@face Logded
注释中。