【发布时间】:2015-07-30 05:18:02
【问题描述】:
这是我的豆子:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Activate annotation configured components -->
<context:annotation-config/>
<!-- Scan components for annotations within the configured package -->
<context:component-scan base-package="document">
<context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
</context:component-scan>
<!-- Define the MongoTemplate which handles connectivity with MongoDB -->
<mongo:mongo host="192.168.0.10" port="27017" />
<mongo:db-factory dbname="yourdb" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
这是我的 DAO
@Repository
public class DocumentRepository {
@Autowired
MongoTemplate mongoTemplate;
def insertDocument(DocumentModel document) {
return mongoTemplate.insert(document)
}
}
每次调用 insert 时都会出现以下错误:
*Timed out after 10000 ms while waiting for a server that matches
AnyServerSelector{}. Client view of cluster state is {type=Unknown,
servers=[{address=localhost:27017, type=Unknown, state=Connecting,
exception={com.mongodb.MongoException$Network: Exception opening the
socket}, caused by {java.net.ConnectException: Connection refused}}];
nested exception is com.mongodb.MongoTimeoutException: Timed out after
10000 ms while waiting for a server that matches AnyServerSelector{}.
Client view of cluster state is {type=Unknown, servers=
[{address=localhost:27017, type=Unknown, state=Connecting, exception=
{com.mongodb.MongoException$Network: Exception opening the socket},
caused by {java.net.ConnectException: Connection refused}}]*
这是我的主文件
@Configuration
@EnableAutoConfiguration
@ComponentScan("document , filter")
class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我在不同的机器上运行 mongodb,即 192.168.0.10。出于某种原因,它一直显示它正在尝试连接到 localhost。有什么我想念的想法吗?
【问题讨论】:
标签: spring mongodb spring-boot host