当前位置: 首页 > 知识库问答 >
问题:

无法解决此问题 不满意依赖性异常:系统导入过程中没有可用于注入的对象

谭正谊
2023-03-14

我已经尝试了很多方法来解决这个问题,但都做不到。我发现,如果我们使用“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);

    }

}

服务:

用户服务及其实施

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";
    }
}

断续器

<?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

为了解决这个问题,我在资源配置中取消了我的绑定,然后我开始使用404。

请帮忙....

其他详情;

Pom公司

<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>

共有2个答案

杜阳泽
2023-03-14

您可以在beans.xml中将bean发现模式设置为all:

<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>

或带有范围注释的annate < code > UserServiceImpl (如< code>@Dependent)

卫博
2023-03-14

使 JAX-RS 资源成为 CDI 源,如下所示:

@Path("/sayHello")
@RequestScoped
public class ControllerTest {

然后你不需要粘合剂,注射应该可以。

 类似资料:
  • 我已经通过 pom 的方式引入了 MyBatis 的依赖,pom 文件的部分内容如下: 但是当我尝试导入包的时候: IDEA 提示“无法解析符号 'ibatis'”,而当我运行程序时,终端提示 org.apache.ibatis.annotations 包不存在。 我该如何解决这个问题?mybatis-spring-boot-starter的依赖应该包含org.apache.ibatis.anno

  • 问题内容: 我想使用适当的依赖项注入来注入对象的字段。我尝试了很多不同的尝试注入等失败的组合。 问题答案: 解析是路由的属性,而不是控制器的属性。控制器将注入在路由级别上定义的依赖项,而无需在控制器上指定解析属性。 以您的一个示例(转换为JavaScript)为例,您将像往常一样定义控制器,即: 然后是路线上的resolve属性: 如果您想使用路由的resolve部分来减少代码,则需要使用数组样式

  • 我正在尝试使用cucumber框架与selenium和appium,但在执行cucumber特性时,我得到以下异常: @CucumberOptions(features={“src//test//java//feature”},glue={“pages”},plugin={“pretty”,“html:target/cucumber”},tags={“@web”,“@test”,“@appium”

  • 然而,Eclipse告诉我“没有bean可以被注入到注入点[JSR-299§5.2.1]”。我做错了什么?你有没有看到我缺少的东西。如有任何帮助,我们将不胜感激。 谢谢!!

  • 供给方组件: 注入方组件: 浏览器中可以正常展示: 但是如果在供给方组件中,更换ElMessage和provide的顺序,如下图: 浏览器中就无法正常渲染,并且控制台报警告: 请问这是什么原因呢? 备注: "element-plus": "^2.3.12" "vue": "^3.3.4"

  • 我正在学习Spring依赖注入。我有两种类型的代码。一个管用一个不管用...但是,他们都为制作教程的人工作。 注释代码给出了如下所示的错误。 Beans.xml 使用ApplicationContext时出现错误消息 线程“main”组织中出现异常。springframework。豆。工厂BeanDefinitionStoreException:IOException解析来自类路径资源[Beans