无法解决这个问题 UnsatisfiedDependencyException: There was no object available for injection at SystemInject
Posted
技术标签:
【中文标题】无法解决这个问题 UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl【英文标题】:Can't solve this issue UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl 【发布时间】:2019-03-01 00:08:39 【问题描述】:我已经尝试了很多来解决这个问题,但不能。我发现,如果我们使用“abstractBinder”,那么这可以解决,但是一旦我的 Binder 就位,我就会开始出现 404 错误。
UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl
请帮忙
我的资源:
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.walmart.services.helpers.IUserService;
import com.walmart.services.helpers.ServicesTest;
@Path("/sayHello")
public class ControllerTest
@Inject
private IUserService service;
@Inject
private ServicesTest service2;
@GET
@Path("/name")
@Produces(MediaType.TEXT_PLAIN)
public String method(@PathParam("name") String msg)
return service.method() + " msg";
@GET
@Path("/v2/name")
@Produces(MediaType.TEXT_PLAIN)
public String method2(@PathParam("name") String msg)
return service2.method() + " msg";
我的资源配置文件:
@ApplicationPath("/rest/*")
public class ResourceConfiguration extends ResourceConfig
public ResourceConfiguration()
//register(new MyBinder());
this.packages(true, "com.walmart.services.*");
我的活页夹 [如果有的话]
public class MyBinder extends AbstractBinder
@Override
protected void configure()
// TODO Auto-generated method stub
bind(new ServicesTest()).to(ServicesTest.class);
// bind(UserServiceImpl.class).to(IUserService.class).in(RequestScoped.class);
服务:
IUserService 及其实现
public interface IUserService
public String method();
public class UserServiceImpl implements IUserService
@Inject
public UserServiceImpl()
System.out.println("test");
@Override
public String method()
return "Welcome ";
其他
public class ServicesTest
public ServicesTest()
System.out.println("created ");
public String method()
return "Welcome";
WEBXML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>com.walmart.learning.javaee</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
现在,我可以使用 http://localhost:8080/javaeeLearning/rest/sayHello/h
这给了我以下错误
SEVERE: Servlet.service() for servlet [com.walmart.configuration.ResourceConfiguration] in context with path [/javaeeLearning] threw exception [A MultiException has 4 exceptions. They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=IUserService,parent=ControllerTest,qualifiers=,position=-1,optional=false,self=false,unqualified=null,2007960340)
2. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=ServicesTest,parent=ControllerTest,qualifiers=,position=-1,optional=false,self=false,unqualified=null,10615079)
3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.walmart.services.rest.controller.ControllerTest errors were found
4. java.lang.IllegalStateException: Unable to perform operation: resolve on com.walmart.services.rest.controller.ControllerTest
] with root cause
为了解决这个问题,我在资源配置中取消注释我的 Binder 然后我开始有 404。
请帮忙....
其他细节;
波姆
<name>javaeeLearning</name>
<properties>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
<jaxrs.version>2.0.1</jaxrs.version>
<jersey2.version>2.23</jersey2.version>
<jersey2.gf.cdi.version>2.14</jersey2.gf.cdi.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>$jaxrs.version</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</dependency>
<!-- Jersey2.x Dependencies -->
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>$jersey2.version</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>$jersey2.version</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>$jersey2.version</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>$jersey2.version</version>
</dependency>
<!-- Jersey2.x Dependency injection -->
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 -->
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-core -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.4.0-b180830.0438</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>$jersey2.version</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers.glassfish/jersey-gf-cdi -->
<dependency>
<groupId>org.glassfish.jersey.containers.glassfish</groupId>
<artifactId>jersey-gf-cdi</artifactId>
<version>$jersey2.gf.cdi.version</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.enterprise/cdi-api -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
【问题讨论】:
你需要活页夹。将注意力集中在为什么要获得 404。您是否为所有端点获得 404?您是否得出了添加 DI 服务会导致 404 的绝对结论?如果您完全删除所有 DI 服务,一切是否运行正常? 【参考方案1】:让您的 JAX-RS 资源成为这样的 CDI bean:
@Path("/sayHello")
@RequestScoped
public class ControllerTest
那么你就不需要 Binder 并且注入应该可以工作。
【讨论】:
是否得到与之前完全相同的异常?如果您不在完整的 Java EE 环境(Tomcat?)中,您可能还需要执行@user3544624 在他的回答中建议的操作。 @PanuHaaramo 您在哪个服务器上运行您发布的代码? 如果您的 beans.xml 中的 bean 发现模式被注释,请使用上下文范围 bean 注释您的 restful 资源端点。谢谢@PanuHaaramo【参考方案2】:您可以在beans.xml
中将 bean 发现模式设置为全部:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
或使用范围注释(如@Dependent
)注释UserServiceImpl
【讨论】:
嗨,谢谢你,但我有这个文件。仍然无法正常工作。 你没有在 Resourceconfig 中注册 binder?appsdeveloperblog.com/dependency-injection-hk2-jersey-jax-rs 请添加appsdeveloperblog.com/dependency-injection-hk2-jersey-jax-rs 提到的初始化参数(sevlet 的配置)。以上是关于无法解决这个问题 UnsatisfiedDependencyException: There was no object available for injection at SystemInject的主要内容,如果未能解决你的问题,请参考以下文章
Error creating bean with name * 错误
我无法解决这个问题:从通过 prompt() 输入的数组中找到最小数
“无法注册输入通道” - 这是由啥引起的以及如何解决这个问题?