我有一个使用Guice和JPA / Hibernate的webapp。我试图设置一个导入器来引入一堆数据,但我一直得到以下异常:
com.google.inject.CreationException: Guice creation errors:||1) Error in custom provider, java.lang.NullPointerException| while locating com.google.inject.persist.jpa.JpaPersistService| while locating javax.persistence.EntityManager| at myApplication.services.SynchronizerService.<init>(SynchronizerService.java:86)| while locating myApplication.services.SynchronizerService| while locating myApplication.api.ISynchronizer| at myApplication.portal.web.PortalWebModule$DataBootstrap.<init>(PortalWebModule.java:137)| at myApplication.portal.web.PortalWebModule.setupDataImport(PortalWebModule.java:83)| while locating myApplication.portal.web.PortalWebModule$DataBootstrap||1 error
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:183)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
at com.google.inject.Guice.createInjector(Guice.java:95)
at com.google.inject.Guice.createInjector(Guice.java:72)
at com.google.inject.Guice.createInjector(Guice.java:62)
at myApplication.portal.web.PortalListener.getInjector(PortalListener.java:36)
at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)
at myApplication.portal.web.PortalListener.contextInitialized(PortalListener.java:24)
at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:775)
at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:424)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:767)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1252)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:710)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:293)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:172)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
at org.eclipse.jetty.server.Server.doStart(Server.java:282)
at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:520)
at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:365)
at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:523)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by:
java.lang.NullPointerException
at com.google.inject.persist.jpa.JpaPersistService.begin(JpaPersistService.java:70)
at com.google.inject.persist.jpa.JpaPersistService.get(JpaPersistService.java:50)
at com.google.inject.persist.jpa.JpaPersistService.get(JpaPersistService.java:34)
at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:55)
at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
at myApplication.services.SynchronizerService$DealerLineProcessor.<init>(SynchronizerService.java:484)
at myApplication.services.SynchronizerService$DealerLineProcessor.<init>(SynchronizerService.java:479)
at myApplication.services.SynchronizerService.<init>(SynchronizerService.java:90)
at myApplication.services.SynchronizerService$$EnhancerByGuice$$3b543af5.<init>(<generated>)
at myApplication.services.SynchronizerService$$EnhancerByGuice$$3b543af5$$FastClassByGuice$$285038d1.newInstance(<generated>)
at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40)
at com.google.inject.internal.ProxyFactory$ProxyConstructor.newInstance(ProxyFactory.java:260)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:54)
at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
at myApplication.portal.web.PortalWebModule$DataBootstrap.refresh(PortalWebModule.java:145)
at myApplication.portal.web.PortalWebModule$DataBootstrap.<init>(PortalWebModule.java:139)
at myApplication.portal.web.PortalWebModule$DataBootstrap$$FastClassByGuice$$8f91c171.newInstance(<generated>)
at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40)
at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:60)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
at com.google.inject.Scopes$1$1.get(Scopes.java:65)
at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:204)
at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:198)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(InternalInjectorCreator.java:198)
at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:179)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
at com.google.inject.Guice.createInjector(Guice.java:95)
at com.google.inject.Guice.createInjector(Guice.java:72)
at com.google.inject.Guice.createInjector(Guice.java:62)
at myApplication.portal.web.PortalListener.getInjector(PortalListener.java:36)
at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)
at myApplication.portal.web.PortalListener.contextInitialized(PortalListener.java:24)
at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:775)
at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:424)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:767)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1252)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:710)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:293)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:172)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
at org.eclipse.jetty.server.Server.doStart(Server.java:282)
at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:520)
at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:365)
at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:523)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
这是有问题的代码。值得注意的部分是configureServlets()
,setupDataImport()
和DataBootstrap
类。
package MyApplication.portal.web;
//imports
/**
* Portal web module
*
* @author mint
*
*/
public final class PortalWebModule extends ServletModule
{
private final Map<String, String> config = new HashMap<String, String>();
private final static Logger LOG = LoggerFactory.getLogger(PortalWebModule.class);
@SuppressWarnings("unused")
private final ServletContext context;
public PortalWebModule(final ServletContext context)
{
this.context = context;
this.config.put("com.sun.jersey.config.feature.DisableWADL", "true");
this.config.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
this.config.put("com.sun.jersey.spi.container.ContainerRequestFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter");
this.config.put("com.sun.jersey.spi.container.ContainerResponseFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter");
}
@Override
protected void configureServlets()
{
install(new WamModule(true));
install(new PortalModule());
install(new PortalResourcesModule());
setupEmbeddedDB();
// Serve it...
serve(PortalConfig.ROUTE_API)
.with(GuiceContainer.class, this.config);
filter("/*")
.through(PersistFilter.class);
// Handle CORS
filter("/*")
.through(CorsFilter.class);
setupDataImport();
}
/**
* Convenience method to setup and start data import with initial data.
*/
private void setupDataImport()
{
this.bind(DataBootstrap.class)
.asEagerSingleton();
LOG.info("Started Data import");
}
/**
* Enable use of embedded DB console.
*/
private void setupEmbeddedDB()
{
if (WamConfig.DEBUG)
{
final Map<String, String> config = new HashMap<String, String>();
config.put("webAllowOthers", "true");
this.bind(WebServlet.class)
.asEagerSingleton();
// Serve it...
serve("/h2*")
.with(WebServlet.class, config);
LOG.warn("Enabling Embedded DB access through browser");
}
}
/**
* This is a helper to start the data service.
*/
//@Singleton
private static class DataBootstrap
{
//This ISynchronizer has the offending EntityManager
private Provider<ISynchronizer> serviceProvider;
@Inject
public DataBootstrap(final Provider<ISynchronizer> serviceProvider)
throws SynchronizationException
{
this.serviceProvider = serviceProvider;
refresh();
}
public void refresh(){
try
{
ISynchronizer service = this.serviceProvider.get();
service.refresh(false);
}
catch (SynchronizationException x)
{
// TODO Auto-generated catch block
x.printStackTrace();
}
}
}
}
我已经读过,当您在调用PersistService.start()
之前尝试使用EntityManager时,通常会出现此错误,但PersistFilter正在执行此操作,如果我尝试显式启动它,则会得到{{ 1}}我也试过玩其他一些策略:在直接实例和提供者之间切换,移动东西,在没有DataBootstrap类的情况下实例化事物......但是我无法在任何地方找到它们
我几乎没有想法......还有其他人知道发生了什么吗?
谢谢,
泰勒
答案 0 :(得分:0)
我找到this question,他提到的解决方法奏效了。基本上,我在其他地方调用了PersistService.start()
,而不是使用默认的PersistFilter,而是使用了一个捕获IllegalStateException
的自定义。