2013-10-10 30 views
0

我正在使用spring 3.0和mongoDB创建示例web应用程序。以下是我的root-context.xml文件。使用弹簧数据创建MongoDB数据库

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <!-- Root Context: defines shared resources visible to all other web components --> 
    <import resource="classpath:mongo-config.xml"/> 

</beans> 

和我蒙戈-config.xml文件是

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:c="http://www.springframework.org/schema/c" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
     xmlns:util="http://www.springframework.org/schema/util" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.1.xsd 
      http://www.springframework.org/schema/data/mongo 
      http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd 
      http://www.springframework.org/schema/util 
      http://www.springframework.org/schema/util/spring-util-3.1.xsd"> 

    <!-- Default bean name is 'mongo'. write concern set to SAFE to ensure unique indexes --> 
    <mongo:mongo host="localhost" port="27017" write-concern="SAFE"/> 

    <mongo:repositories base-package="com.dashboard.repositories" /> 

    <bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory"> 
     <constructor-arg name="mongo" ref="mongo"/> 
     <constructor-arg name="databaseName" value="DASHBOARD"/> 
     <constructor-arg name="credentials" ref="userCredentials"/> 
    </bean> 

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
     <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> 
     <property name="writeResultChecking"> 
      <util:constant static-field="org.springframework.data.mongodb.core.WriteResultChecking.EXCEPTION" ></util:constant> 
     </property> 
    </bean> 

    <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials"> 
     <constructor-arg name="username" value="test"/> 
     <constructor-arg name="password" value="abc123"/> 
    </bean> 

</beans> 

当我启动Tomcat服务器,没有数据库 “仪表板” 创建。这段代码有什么问题,请帮助我。提前致谢。

+0

您是否尝试在您的应用程序中插入任何内容。与RDMS不同,Mongo不提前创建数据库模式。 – fgakk

+0

是的,当我添加注册表单的用户,它创建数据库并添加用户。但是,一旦服务器启动并初始化mongoDbFactory bean,它是否应该创建一个数据库。 – Shahzeb

+1

选中此项:http://docs.mongodb.org/manual/faq/fundamentals/#do-mongodb-databases-have-schemas架构是动态和异构的,因此mongo无法在插入任何数据之前创建一个固定架构作为RDMS。 – fgakk

回答

0

为了让其他观众可见,我会将我的评论放入答案中。在Mongo架构中是动态和异构的,所以mongo在插入任何数据之前不能创建一个固定的架构作为RDMS。 Here是一个更详细的解释。