注入实例上的Guice nullpointer异常 [英] Guice nullpointer exception on injected instance

查看:115
本文介绍了注入实例上的Guice nullpointer异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey2中的Guice进行DI(我想使用它,以便可以使用Google App Engine->不适用于HK2).

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

我的ApplicationResource:

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加载了一堆文件-并且文件已正确加载. 但是当我尝试注入NLPProvider的实例时,它保持为空.

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.

Crwaler资源:

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();
    }

}

考虑到我以前使用过HK2,我在做什么?

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

我的pom.xml-也许问题出在这里:

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>

推荐答案

您需要将guice与HK2桥接,如此处.

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

您还需要HK2 guice-bridge依赖项

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>

这篇关于注入实例上的Guice nullpointer异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆