Home Articles

Spring MVC MongoDB

Asked
Viewed 1224 times
0

我在使用Spring Tool Suite的servlet-context.xml中出现以下错误:

元素bean bean的前缀bean未绑定错误在以下行:http://www.springframework.org/schema/beans/spring-beans-3.0.xsd“>

顺便说一句,我正在尝试使用以下配置将Spring连接到MongoDB:

<?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>

2 Answers

  • 1

    您的开始和结束标记不匹配,您忘了定义 beans 名称空间:

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

    所以,用以下代替第一行:

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

    另外,将 mvc 名称空间添加为默认名称空间:

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

    你的 servlet-context.xml 最终看起来像这样:

    <?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>
    

    如果您发现自己使用Spring配置会感到不舒服,我猜你会这样做,最好切换到 Spring Boot ,它会自动完成大部分这类配置 .

  • 1

    这是一个简单的mongo配置

    @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;
        }
    
    }
    

    它使用注释,但它与你只是@Autowire mongoTemplate相同 .

    转换为xml,这看起来与此类似或相等

    <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>
    

    没有在xml上测试过,所以我可能会遗漏一些东西,

    哦,正如另一个答案所指出的那样,你应该 <beans:bean ..... 它应该是 <beans:bean ..... 并且恰当地关闭 </beans:bean>

    希望能帮助到你

Related