Java 注入实例上的Guice nullpointer异常
我正在Jersey2中为DI使用Guice(我想使用它,这样我就可以使用Google App Engine->不使用HK2) 我的应用程序资源:Java 注入实例上的Guice nullpointer异常,java,jersey,guice,jersey-2.0,hk2,Java,Jersey,Guice,Jersey 2.0,Hk2,我正在Jersey2中为DI使用Guice(我想使用它,这样我就可以使用Google App Engine->不使用HK2) 我的应用程序资源: public class ApplicationResource extends ResourceConfig { private static final Logger LOGGER = null; public ApplicationResource() { System.out.println("Applica
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的实例时,它保持为空
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,我做错了什么
My pom.xml-问题可能来自这里:
<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>
4.0.0
com.crawler
C_API
战争
1.0-快照
C_API
C_API
org.apache.maven.plugins
maven编译器插件
3.2
真的
1.7
1.7
org.glassfish.jersey
针织面料
${jersey.version}
聚甲醛
进口
org.glassfish.jersey.containers
jersey容器servlet
org.glassfish.jersey.media
泽西媒体公司
com.zaxxer
HikariCP-java6
2.2.5
mysql
mysql连接器java
5.1.36
通用编解码器
通用编解码器
1.9
org.codehaus.jackson
杰克逊地图绘制者
1.9.13
org.mindrot
jbcrypt
0.3米
net.sf.json-lib
json库
2.4
jdk15
com.synchemall
锅炉管
1.2.1
薛西斯
干细胞移植
2.11.0
net.sourceforge.nekohtml
内科特米尔
1.9.22
org.apache.opennlp
opennlp工具
1.6.0
番石榴
番石榴
14.0.1
罐子
log4j
log4j
1.2.17
com.github.mpkorstanje
simmetrics
3.2.1
com.cybozu.labs
朗德特
1.1-20120112
org.jsoup
jsoup
1.8.3
com.google.inject
圭斯
3
2.21
UTF-8
如前所述,您需要将guice与HK2连接起来
您还需要HK2guice桥
依赖项
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>guice-bridge</artifactId>
<version>2.4.0-b31</version>
</dependency>
org.glassfish.hk2
圭斯桥
2.4.0-b31
谢谢-这为guice解决了我的问题。但由于我仍然在使用HK2,它不能与谷歌应用程序引擎一起工作。我知道这将是另一个问题——但仍然——对此有什么建议吗?我不知道,我从未使用过GAE,所以我不知道它是如何工作的?春天怎么样?不确定如何/是否可以在不涉及Hk2的情况下使用Jersey 2和Guice。我知道泽西1号你可以。是的,这有点困难。我现在要看看弹性豆茎。谢谢你的帮助:)
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>guice-bridge</artifactId>
<version>2.4.0-b31</version>
</dependency>