quarkus CDI:拦截器没有绑定
quarkus CDI: Interceptor has no bindings
我严格按照这里的教程(https://quarkus.io/guides/cdi#interceptors), and write a simple interceptor based on this mini example (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 has no bindings
错误:
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.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)
我做错了什么吗?
缺少的部分是将 @InterceptorBinding
注释添加到 public @interface Logged
注释。
我严格按照这里的教程(https://quarkus.io/guides/cdi#interceptors), and write a simple interceptor based on this mini example (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 has no bindings
错误:
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.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)
我做错了什么吗?
缺少的部分是将 @InterceptorBinding
注释添加到 public @interface Logged
注释。