2013-10-10 4 views
0

Я создаю примерное веб-приложение с пружиной 3.0 и mongoDB. Ниже приведен мой файл 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

Да, когда я добавляю пользователя с формой регистрации, он создает db и добавляет к нему пользователя. Но следует ли создавать базу данных сразу после запуска сервера и инициализировать компонент mongoDbFactory. – Shahzeb

+1

Проверьте это: http://docs.mongodb.org/manual/faq/fundamentals/#do-mongodb-databases-have-schemas Схемы являются динамическими и гетерогенными, поэтому mongo не может создать фиксированную схему как RDMS, прежде чем вставлять какие-либо данные. – fgakk

ответ

0

Чтобы сделать его видимым для других зрителей, я поместил свой комментарий в ответ. В схемах Mongo динамические и гетерогенные, поэтому mongo не может создать фиксированную схему как RDMS, прежде чем вставлять какие-либо данные. Here - более подробное объяснение.

Смежные вопросы