Ahmed-F
Ahmed-F

Reputation: 147

Spring MVC + MongoDB

I am having the following error at my servlet-context.xml with Spring Tool Suite :

the prefix beans for element beans bean is not bound The error is at the line : http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

By the way, i am trying to connect Spring to MongoDB with following configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans:bean xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:mongo="http://www.springframework.org/schema/data/mongo"
          xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.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/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.etvld.mvc" />

    <mongo:mongo host="192.168.1.19" port="27017"/>

</beans:beans>

Upvotes: 0

Views: 922

Answers (2)

Ali Dehghani
Ali Dehghani

Reputation: 48193

Your opening and ending tags does not match and you forgot to define beans namespace:

<?xml version="1.0" encoding="UTF-8"?>
<beans:bean ...>
       ^^^^ opening tag => bean
...    
</beans:beans>
    |   ^^^^^ ending tag => beans
    |
    + =====> Where did you define beans namespace?

So, replace the fist line with following:

<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"

Also, add the mvc namespace as the default namespace:

xmlns="http://www.springframework.org/schema/mvc"

Your servlet-context.xml would look like this in the end:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:mongo="http://www.springframework.org/schema/data/mongo"
             xmlns="http://www.springframework.org/schema/mvc"
             xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.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/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.etvld.mvc" />

    <mongo:mongo host="192.168.1.19" port="27017"/>

</beans:beans>

If you finding yourself uncomfortable using Spring configurations, which i guess you do, it's better to switch to Spring Boot, which does most of these kind of configurations automatically.

Upvotes: 1

jstuartmilne
jstuartmilne

Reputation: 4508

Here is a simple mongo config

@Configuration
public class SpringConfig {

    @Bean
    public MongoDbFactory mongoDbFactory() throws UnknownHostException{
        return new SimpleMongoDbFactory(new MongoClient(),"games");
    }

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

}

It uses annotations but its the same thing then you just @Autowire mongoTemplate.

Translated to xml this would look something similar or equal to this

    <beans:bean id="mongoClient" class="com.mongodb.MongoClient">
    </beans:bean>

<beans:bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
      <constructor-arg ref="mongoClient"/>
      <constructor-arg type="java.lang.String" value="dbName"/>
   </beans:bean>

<beans:bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
      <constructor-arg ref="mongoDbFactory"/>

   </beans:bean>

Haven't tested on xml though so I could be missing something,

Oh as the other answer pointed out you are putting <beans:beans it should be <beans:bean ..... and close accordigly </beans:bean>

Hope it helps

Upvotes: 1

Related Questions