英文:
No bean is eligible for injection to the injection point[JSR-365 5.2.2]
问题
大多数时候,在基于MicroProfile的微服务中使用@Inject
注解时,我会收到警告信息:“No bean is eligible for injection to the injection point[JSR-365 5.2.2]”。这个警告的原因是什么,以及如何克服它呢?举个例子,我编写了一个用于属性文件注入的代码:
@Path("/configProperty")
@Singleton
public class ConfigPropertyResource {
@Inject
@ConfigProperty(name = "username")
private String username;
@GET
@Path("/mp-config")
@Produces(MediaType.APPLICATION_JSON)
public Response mpConfig() {
Map<String, Object> configProperties = new HashMap<>();
return Response.ok(configProperties).build();
}
}
现在,在@Inject
注解处,它显示了一个警告标志,建议说:“No bean is eligible for injection to the injection point[JSR-365 5.2.2]”。
我正在使用MicroProfile版本3.3和Wildfly 19.1作为运行时。
注意:通过添加@SuppressWarnings("cdi-ambiguous-dependency")
,警告会消失,但这似乎没有意义。
英文:
Most of the time I use @Inject annotation in MicroProfile based microservices I get "No bean is eligible for injection to the injection point[JSR-365 5.2.2]" as a warning. What is the reason for this warning and what can be done in order to overcome it? Say for example. I wrote a code for property file injection-
@Path("/configProperty")
@Singleton
public class ConfigPropertyResource{
@Inject
@ConfigProperty(name = "username")
private String username;
@GET
@Path("/mp-config")
@Produces(MediaType.APPLICATION_JSON)
public Response mpConfig() {
Map<String, Object> configProperties = new HashMap<>();
return Response.ok(configProperties).build();
}
}
Now at @Inject annotation, it shows a warning sign with the suggestion-No bean is eligible for injection to the injection point[JSR-365 5.2.2].
I am using Microprofile version 3.3 with wildfly 19.1 as run time.
Note: By adding @SuppressWarnings("cdi-ambiguous-dependency"), it's gone, but it didn't make sense.
答案1
得分: 1
我假设这是您正在使用的IDE的问题。由于Eclipse MicroProfile Config使用了众所周知的CDI注解,如@Inject
,因此您的IDE会尝试为您提供检查是否存在可注入的CDI bean的支持。IDE的这种CDI支持可能无法识别您实际上正在使用Eclipse MicroProfile,因此经典的CDI支持认为您想要注入另一个bean而不是属性。
如果在运行时没有遇到任何问题,只是您的IDE在抱怨,要么更新IDE(如果支持MicroProfile Config),要么忽略/抑制此警告。
我正在使用IntelliJ IDEA 2020.2,开发Eclipse MicroProfile Config应用程序时并没有遇到这样的问题。
英文:
I assume that's an issue of the IDE you are using. As Eclipse MicroProfile Config uses the well-known CDI annotations like @Inject
, your IDE tries to provide you support for checking if there is a CDI bean available to inject. This CDI support of the IDE might not recognize that you actually use Eclipse MicroProfile and hence the classic CDI support is thinking you want to inject another bean but not a property.
If you don't encounter any issue during runtime and it's just you IDE that's complaining, either update the IDE (if it supports MicroProfile Config) or ignore/suppress this warning.
I'm using IntelliJ IDEA 2020.2 and I don't encounter such issues while developing applicaitons with Eclipse MicroProfile Config.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论