Quarkus中Kotlin与Java互操作问题:CDI及EventBus异常排查
Quarkus Java应用引入Kotlin类的CDI与EventBus问题
案例1 - EventBus事件未触发问题
Kotlin事件消费类代码:
@ApplicationScoped class GreetingServiceBus { @ConsumeEvent("KotlinTest") fun greeting(name: String) { System.out.println("hello $name"); } }
Java端点调用代码:
@Path("/api") public class ApiExecute { @Inject EventBus eventBus; @POST @Path("/execute") public Response execute() { eventBus.send("KotlinTest", "Hello"); } }
问题:调用eventBus.send发送事件后,Kotlin类中的greeting方法从未被触发,但使用Java类标注@ConsumeEvent("KotlinTest")时可正常接收事件。
案例2 - CDI注入失败问题
Kotlin服务类代码:
@ApplicationScoped class GreetingService { fun greeting(name: String): String { return "hello $name" } }
Java端点注入代码:
@Path("/api") public class ApiExecute { @Inject GreetingService greetingService; }
编译报错信息:
error: cannot find symbol GreetingService greetingService;
补充说明:已知CDI可使用@field:Default注解,但该注解仅适用于Kotlin代码;尝试构造注入也遇到相同错误,代码如下:
@POST @Path("/execute") @Inject public Response execute(GreetingService greetingService, String input) { }
内容的提问来源于stack exchange,提问作者Charlie
相关产品推荐
相关产品推荐

