Guice nullpointer exception on injected instance

六眼飞鱼酱① 提交于 2019-12-10 18:46:51

问题


I'm using Guice in Jersey2 for DI (i want to use it so i can use Google App Engine -> not working with HK2).

My ApplicationResource:

public class ApplicationResource extends ResourceConfig {

    private static final Logger LOGGER = null;

    public ApplicationResource() {
        System.out.println("Application startup");
        // Register resources and providers using package-scanning.
        packages("com.crawler.c_api");

        // Register my custom provider - not needed if it's in my.package.
        register(ResponseCorsFilter.class);

        Injector createInjector = Guice.createInjector(new AbstractModule(){

            @Override
            protected void configure() {
                bind(NLPProvider.class).toInstance(new NLPProvider());
            }

        });


        // Register an instance of LoggingFilter.
        register(new LoggingFilter(LOGGER, true));

        // Enable Tracing support.
        property(ServerProperties.TRACING, "ALL");        

        EncodingFilter.enableFor(this, GZipEncoder.class);
    }
}

NLPProvider loads a bunch of files - and the files are loaded correctly. But when i try to inject the Instance of NLPProvider it stay null.

CrwalerResource:

@Path("/crawler")
public class CrawlerResource {

    @Inject
    NLPProvider pipeline;

    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response crawlUrl(@BeanParam CrawlerQueryParam queryParameters) {
        CrawlerService crawl = new CrawlerService();
        return Response.status(200).entity(crawl.extractFromUrl(queryParameters, pipeline)).build();
    }

}

What am i doing wrong considering that i used HK2 before?


My pom.xml - maybe the issue comes from here:

<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.crawler</groupId>
    <artifactId>C_API</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>C_API</name>

    <build>
        <finalName>C_API</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <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-servlet</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>


        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP-java6</artifactId>
            <version>2.2.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.mindrot</groupId>
            <artifactId>jbcrypt</artifactId>
            <version>0.3m</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>com.syncthemall</groupId>
            <artifactId>boilerpipe</artifactId>
            <version>1.2.1</version>
        </dependency>

        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.11.0</version>
        </dependency> 
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>

        <dependency>
            <groupId>org.apache.opennlp</groupId>
            <artifactId>opennlp-tools</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>14.0.1</version>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.github.mpkorstanje</groupId>
            <artifactId>simmetrics</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.cybozu.labs</groupId>
            <artifactId>langdetect</artifactId>
            <version>1.1-20120112</version>
        </dependency>

        <dependency>
            <!-- jsoup HTML parser library @ http://jsoup.org/ -->
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.8.3</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>3.0</version>
        </dependency>



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

回答1:


You will need to bridge guice with HK2, as mentioned here.

@Inject
public ApplicationResource(ServiceLocator locator) {
    GuiceBridge.getGuiceBridge().initializeGuiceBridge(locator);
    // add your Guice modules.
    Injector injector = Guice.createInjector(new GuiceModule());
    GuiceIntoHK2Bridge guiceBridge = locator.getService(GuiceIntoHK2Bridge.class);
    guiceBridge.bridgeGuiceInjector(injector);
}

You'll also need the HK2 guice-bridge dependency

<dependency>
    <groupId>org.glassfish.hk2</groupId>
    <artifactId>guice-bridge</artifactId>
    <version>2.4.0-b31</version>
</dependency>


来源:https://stackoverflow.com/questions/32609954/guice-nullpointer-exception-on-injected-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!