Google Guice 辅助注入对象为空
Google Guice Assisted Inject object is null
我需要在 runtime.TO 使用用户定义的数据创建对象,我已经使用过
google guice 协助 inject.But 当我 运行 我的测试它抛出 null
指针 exception.Please 让我知道我在哪里犯了错误。
IArtifacts 界面
public interface IArtifacts {
MavenMetaDataXMLDTO getArtifactsVersions();
}
ArtifactsService.java
public class ArtifactsService implements IArtifacts {
private ProductProfile productProfile;
@Inject
public ArtifactsService(@Assisted ProductProfile productProfile){
System.out.println(productProfile.getArtifactManagementURL());
this.productProfile=productProfile;
}
@Override
public MavenMetaDataXMLDTO getArtifactsVersions() {
System.out.println(productProfile.getArtifactManagementURL());
return null;
}
}
ArtifactsFactory 接口
public interface ArtifactsFactory {
IArtifacts create(ProductProfile productProfile);
}
模块Class
@Override
protected void configure() {
install(new FactoryModuleBuilder().implement(IArtifacts.class,ArtifactsService.class).build(ArtifactsFactory.class));
}
TestArtifacts.java
public class TestArtifacts {
@Inject // this obj is null
private ArtifactsFactory artifactsFactory;
private IArtifacts s;
public TestArtifacts(){
}
public void getdata(){
//Pass custom data to factory
this.s=artifactsFactory.create(Products.QA.get());
System.out.println(s.getArtifactsVersions());
}
}
REST 端点
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public String getartifacts(){
new TestArtifacts().getdata();
}
您在 Rest Endpoint class 中自己创建了 class TestArtifacts 的实例,但是您所有的 classes 都需要由 Guice Framework 创建,而不是由 Guice Framework 创建你。
那么,当您使用 new 创建它们时,Guice 框架应该如何向您的 class 中注入一些东西?您还需要将 class TestArtifacts 注入您的 Rest Endpoint,并且您的 Rest Endpoint 也必须由 Guice 创建。
更新:
也许这个link会对你有所帮助
https://sites.google.com/a/athaydes.com/renato-athaydes/posts/jersey_guice_rest_api
我能够修复它,将以下代码片段添加到下面 TestArtifacts.java class
TestArtifacts.java
private Injector injector=Guice.createInjector(new MYModule());//where implemented configuration
@Inject
private ArtifactsFactory artifactsFactory=injector.getInstance(ArtifactsFactory.class);
我需要在 runtime.TO 使用用户定义的数据创建对象,我已经使用过
google guice 协助 inject.But 当我 运行 我的测试它抛出 null
指针 exception.Please 让我知道我在哪里犯了错误。
IArtifacts 界面
public interface IArtifacts {
MavenMetaDataXMLDTO getArtifactsVersions();
}
ArtifactsService.java
public class ArtifactsService implements IArtifacts {
private ProductProfile productProfile;
@Inject
public ArtifactsService(@Assisted ProductProfile productProfile){
System.out.println(productProfile.getArtifactManagementURL());
this.productProfile=productProfile;
}
@Override
public MavenMetaDataXMLDTO getArtifactsVersions() {
System.out.println(productProfile.getArtifactManagementURL());
return null;
}
}
ArtifactsFactory 接口
public interface ArtifactsFactory {
IArtifacts create(ProductProfile productProfile);
}
模块Class
@Override
protected void configure() {
install(new FactoryModuleBuilder().implement(IArtifacts.class,ArtifactsService.class).build(ArtifactsFactory.class));
}
TestArtifacts.java
public class TestArtifacts {
@Inject // this obj is null
private ArtifactsFactory artifactsFactory;
private IArtifacts s;
public TestArtifacts(){
}
public void getdata(){
//Pass custom data to factory
this.s=artifactsFactory.create(Products.QA.get());
System.out.println(s.getArtifactsVersions());
}
}
REST 端点
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public String getartifacts(){
new TestArtifacts().getdata();
}
您在 Rest Endpoint class 中自己创建了 class TestArtifacts 的实例,但是您所有的 classes 都需要由 Guice Framework 创建,而不是由 Guice Framework 创建你。
那么,当您使用 new 创建它们时,Guice 框架应该如何向您的 class 中注入一些东西?您还需要将 class TestArtifacts 注入您的 Rest Endpoint,并且您的 Rest Endpoint 也必须由 Guice 创建。
更新:
也许这个link会对你有所帮助
https://sites.google.com/a/athaydes.com/renato-athaydes/posts/jersey_guice_rest_api
我能够修复它,将以下代码片段添加到下面 TestArtifacts.java class
TestArtifacts.java
private Injector injector=Guice.createInjector(new MYModule());//where implemented configuration
@Inject
private ArtifactsFactory artifactsFactory=injector.getInstance(ArtifactsFactory.class);