我正在使用maven创建一个基本的Web应用程序,然后导入到Eclipse 4.2.我将Tomcat 7设置为服务器.我正在尝试使用mongodb为Web应用程序配置spring数据.
我遵循这里找到的基于代码的配置方法:WebApplicationInitializer
当我在服务器上运行项目时,我在我创建的WebApplicationInitializer类中得到一个空指针异常.行:container.addServlet("dispatcher",new DispatcherServlet(dispatcherContext)); 返回null.
我错过了什么?我使用注释从头开始创建Web应用程序有点新鲜.
这是有问题的课程:
public class ATWWebAppInitializer implements WebApplicationInitializer
{
@Override
public void onStartup(ServletContext container) throws ServletException
{
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(SpringMongoConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(ATWDispatcherConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
}
}
Run Code Online (Sandbox Code Playgroud)
尝试将此添加到POM: …