我在servlet-context.xmlSpring工具套件时出现以下错误:
元素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>
下面是一个简单的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进行测试,所以我可能遗漏了一些东西,
哦,正如另一个答案指出的那样,你正在把
希望有帮助
您的开始和结束标签不匹配,并且您忘记定义bean
命名空间:
<?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上下文。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,它会自动执行大部分此类配置。