2016-02-25 31 views
0

我有我的servlet-context.xml中下面的错误与Spring工具套件:的Spring MVC + MongoDB的

前缀豆元素豆豆不绑定的错误是在 行: http://www.springframework.org/schema/beans/spring-beans-3.0.xsd“>

顺便说一句,我试图用春以下配置连接到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

是否有一个特别的原因,你使用的是过时的版本? – chrylis

+0

我只是在尝试Spring.never以前用过它:/ –

+0

然后你会遇到很多不必要的麻烦。看看Spring入门指南和start.spring.io。 – chrylis

回答

0

你的开启和结束标记不匹配,你忘了定义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的配置文件,这我猜你发现自己不舒服,最好切换到春天引导,它会自动完成大部分这几样配置的。

+1

感谢您的帮助,我将转而使用Spring Boot;) –

+0

这更好.Spring Boot将Spring应用程序的大部分典型繁重内容摘要化,这有助于您更加轻松地使用该框架。祝你好运,玩得开心,也远离'xml':)) –

+0

嘿嘿好吧;) –

1

下面是一个简单蒙戈配置

@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:beans 应该<beans:bean .....并关闭accordigly </beans:bean>

希望它可以帮助