2015-11-04 171 views
1

當我在我的web應用程序添加MongoDB的配置,我得到這些錯誤:Spring MVC的MongoDB的配置

ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed 
java.lang.IllegalArgumentException: ResourceLoader must not be null! 
    at org.springframework.util.Assert.notNull(Assert.java:112) 
    at org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport.registerBeanDefinitions(RepositoryBeanDefinitionRegistrarSupport.java:65) 
    at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:331) 
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:228) 
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:149) 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:126) 
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:219) 
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:149) 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:135) 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:260) 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:203) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:617) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446) 
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) 
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) 
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) 
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) 
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231) 
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144) 
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:817) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 
Nov 04, 2015 10:21:47 PM org.apache.catalina.core.ApplicationContext log 
SEVERE: StandardWrapper.Throwable 
java.lang.IllegalArgumentException: ResourceLoader must not be null! 
    at org.springframework.util.Assert.notNull(Assert.java:112) 
    at org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport.registerBeanDefinitions(RepositoryBeanDefinitionRegistrarSupport.java:65) 
    at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:331) 
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:228) 
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:149) 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:126) 
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:219) 
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:149) 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:135) 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:260) 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:203) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:617) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446) 
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) 
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) 
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) 
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) 
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231) 
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144) 
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:817) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 

Nov 04, 2015 10:21:47 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Allocate exception for servlet appServlet 
java.lang.IllegalArgumentException: ResourceLoader must not be null! 
    at org.springframework.util.Assert.notNull(Assert.java:112) 
    at org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport.registerBeanDefinitions(RepositoryBeanDefinitionRegistrarSupport.java:65) 
    at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:331) 
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:228) 
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:149) 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:126) 
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:219) 
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:149) 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:135) 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:260) 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:203) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:617) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446) 
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) 
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) 
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) 
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) 
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231) 
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144) 
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:817) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 

我已經將低於彈簧數據的MongoDB:

<dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-mongodb</artifactId> 
     <version>1.8.0.RELEASE</version> 
    </dependency> 

MongoConfiguration低於:

@Configuration 
@EnableMongoRepositories(basePackages = { "com.eraytuncer.store.repository" }) 
public class MongoConfiguration extends AbstractMongoConfiguration { 

    public @Bean Mongo mongo() throws UnknownHostException { 
     ServerAddress serverAddress = new ServerAddress("localhost", 27017); 
     MongoCredential credential = MongoCredential.createMongoCRCredential("storeUser", getDatabaseName(), "storePass".toCharArray()); 
     MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(4).socketKeepAlive(true).build(); 
     Mongo mongo = new MongoClient(serverAddress, Arrays.asList(credential), options); 
     // mongo.setWriteConcern(WriteConcern.SAFE); 
     return mongo; 
    } 

    @Bean(name = "MongoTemplate") 
    public MongoTemplate mongoTemplate() throws Exception { 
     return new MongoTemplate(mongo(), getDatabaseName()); 
    } 

    @Override 
    protected String getDatabaseName() { 
     return "store"; 
    } 

} 

我有春天版本3.1.1.RELEASE。它已被配置爲javaconfig。

回答

1

我已將Spring框架版本從3.1.1.RELEASE切換到4.2.2.RELEASE,並且這些錯誤消失了。

3
@Configuration 
public class Config(){ 
@Bean 
public MongoDbFactory mongoDbFactory() throws UnknownHostException{ 
return new SimpleMongoDbFactory(new MongoClient(new MongoClientURI(protocol://username:[email protected]/databasename)),"Database_name_here"); //mongodb is the protocol 
}  

@Bean 
public MongoTemplate mongoTemplate() throws UnknownHostException{ 
return new MongoTemplate(mongoDbFactory); 
} 
} 

當你想在一個主類,以引導這個配置,你會做,通過使用Spring的AnnotationConfigApplicationContext這樣的:

ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); 
MongoOperations template = context.getBean(MongoTemplate.class); 

然後,你會調用所有MongoTemplate對MongoOperations的實例方法。