4

GlassFish にデプロイする単純なテストアプリケーションがあります (1 つの単純なクラスが注入された 1 つのリソースのみ)。GlassFish では、すべて正常に動作しています (FINE をログに記録するときの例外を除きます。ここを参照してください)。リソースを正常に呼び出すことができ、Weld CDI が注入を行っています。

Java SE でアプリケーションを起動して JUnit で単体テストを実行すると、HK2 がインジェクションを提供しようとしているように見え、それが UnsatisfiedDependencyException につながります。

InjectMe クラスをバインドする AbtractBinder を定義して、HK2 が認識できるようにします。その後、注射はうまくいきます。ただし、Weld CDI は、クラスを HK2 ではなく、Jersey リソースに挿入する必要があります。

ブートストラップは、カスタム JUnit ランナー (WeldJUnit4Runner) とアノテーションを作成することによって行われます@RunWith(WeldJUnit4Runner.class)

Weld CDI は Java SE 環境で正常にブートストラップされ、Jersey リソースではなく、実際の JUnit テストクラスに InjectMe クラスを正常に挿入できます。InjectMe クラスは、Weld ブートストラップによって検出されます。

2014-07-16 10:41:12,730 [DEBUG] org.jboss.weld.Bootstrap - WELD-000106: Bean: Managed Bean [class com.test.rest.InjectMe] with qualifiers [@Any @Default]

質問: Java SE で実行しているときに、HK2 が Weld ではなく InjectMe クラスを注入しようとするのはなぜですか?

編集: Netbeans プロジェクトを作成したので、このテストを簡単に再現できます: simple-service.zip。java ロギングのプロパティを logging.properties ファイルに設定することを忘れないでください。

java.util.logging.config.file=C:\glassfish4\glassfish\domains\domain1\config\logging.properties

これは、Netbeans の「テスト ファイル」アクション用に定義することで実現しました。プロパティ ファイルで、次のフラグを定義する必要があります。

org.glassfish.jersey.level=FINEST

昨日テストしていたとき、この正確なセットアップが実際に機能する時がありましたが、それらの例外はありませんでした. HK2が最初に初期化されることがある静的初期化に問題があると思いますが、これがこのエラーにつながると思います。

リソース:

package com.test.rest;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Path("rest/import")
public class DataImport {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Inject
    private InjectMe me;

    public DataImport() {}

    @GET
    @Produces("text/html")
    public String getHtml() {
        logger.debug("getHtml of DataImport called!");
        return "<html><body><h1>IMPORT Resource: " + me.field + " </body></h1></html>";
    }
}

アプリケーション構成:

package com.test.rest;

import java.util.logging.Logger;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;

@javax.ws.rs.ApplicationPath("/rest")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(DataImport.class);
        register(RolesAllowedDynamicFeature.class);

        //tracing/logging
        property("jersey.config.server.tracing.type", "ALL"); //enable tracing support, logs requests
        registerInstances(new LoggingFilter(Logger.getLogger(ApplicationConfig.class.getName()), true));
    }
}

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <packaging>war</packaging>
    <version>0.1-SNAPSHOT</version>
    <name>test-service</name>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-http</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-servlet</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>  
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se</artifactId>
           <version>2.2.1.Final</version>
           <scope>test</scope>
        </dependency>  
         <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <scope>provided</scope> 
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope> 
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-war-plugin</artifactId> 
                <version>2.3</version> 
                <configuration> 
                    <failOnMissingWebXml>false</failOnMissingWebXml> 
                </configuration> 
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.example.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <jersey.version>2.8</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

例外:

Jul 16, 2014 10:41:13 AM org.glassfish.jersey.server.ServerRuntime$Responder mapException
FINER: Starting mapping of the exception.
MultiException stack 1 of 3
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=InjectMe,parent=DataImport,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,14097384)
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
    at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:232)
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:255)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:414)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
    at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:160)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:621)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:606)
    at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:173)
    at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
    at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
    at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:94)
    at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:63)
    at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:261)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:252)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1023)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:364)
    at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:217)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:724)
MultiException stack 2 of 3
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.test.rest.DataImport errors were found
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:267)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:414)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
    at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:160)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:621)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:606)
    at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:173)
    at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
    at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
    at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:94)
    at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:63)
    at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:261)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:252)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1023)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:364)
    at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:217)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:724)
MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: resolve on com.test.rest.DataImport
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:443)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
    at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:160)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:621)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:606)
    at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:173)
    at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
    at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
    at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:94)
    at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:63)
    at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:261)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:252)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1023)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:364)
    at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:217)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:724)

注入されたクラス:

package com.test.rest;

public class InjectMe {
    public int field;
    public InjectMe()
    {
        field = 666;
    }
}

使用しています: Glassfish 4.0 (ビルド 89) (Java EE 7 Web)/NetBeans 8.0/maven 3.2.1/JDK 1.7/Weld 2.2.1

4

1 に答える 1

1

jersey glassfish cdi ブリッジを追加してみてください:

<dependency>
  <groupId>org.glassfish.jersey.containers.glassfish</groupId>
  <artifactId>jersey-gf-cdi</artifactId>
  <version>2.11</version>
</dependency>
于 2014-08-21T20:28:35.860 に答える