我的设置在本地有效,但在将其部署到 CloudFoundry/mongolab 时无效。
配置非常类似于docs.
我本地的 spring 配置
@Configuration
@Profile("dev")
@EnableMongoAuditing
@EnableMongoRepositories(basePackages = "com.foo.model")
public class SpringMongoConfiguration extends AbstractMongoConfiguration {
@Override
protected String getDatabaseName() {
return "myDb";
}
@Override
public Mongo mongo() throws Exception {
return new MongoClient("localhost");
}
@Bean
public AuditorAware<User> myAuditorProvider() {
return new SpringSecurityAuditorAware();
}
}
这是 Cloud Foundry 设置
@Configuration
@Profile("cloud")
@EnableMongoAuditing
@EnableMongoRepositories(basePackages = "com.foo.model")
public class SpringCloudMongoDBConfiguration extends AbstractMongoConfiguration {
private Cloud getCloud() {
CloudFactory cloudFactory = new CloudFactory();
return cloudFactory.getCloud();
}
@Bean
public MongoDbFactory mongoDbFactory() {
Cloud cloud = getCloud();
MongoServiceInfo serviceInfo = (MongoServiceInfo) cloud.getServiceInfo(cloud.getCloudProperties().getProperty("cloud.services.mongo.id"));
String serviceID = serviceInfo.getId();
return cloud.getServiceConnector(serviceID, MongoDbFactory.class, null);
}
@Override
protected String getDatabaseName() {
Cloud cloud = getCloud();
return cloud.getCloudProperties().getProperty("cloud.services.mongo.id");
}
@Override
public Mongo mongo() throws Exception {
Cloud cloud = getCloud();
return new MongoClient(cloud.getCloudProperties().getProperty("cloud.services.mongo.connection.host"));
}
@Bean
public MongoTemplate mongoTemplate() {
return new MongoTemplate(mongoDbFactory());
}
@Bean
public AuditorAware<User> myAuditorProvider() {
return new SpringSecurityAuditorAware();
}
}
当我尝试在 Cloud Foundry 中保存文档时遇到的错误是:
OUT ERROR: org.springframework.data.support.IsNewStrategyFactorySupport - Unexpected error
OUT java.lang.IllegalArgumentException: Unsupported entity com.foo.model.project.Project! Could not determine IsNewStrategy.
OUT at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:739)
OUT at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
OUT at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
有任何想法吗?是我的配置文件等吗?
提前致谢
尼克拉斯
这通常是由于为实体获取的 Mongo 映射元数据在应用程序启动时未扫描实体而导致的。默认情况下,AbstractMongoConfiguration
使用实际配置类的包来查找@Document
启动时带注释的类。
异常消息让我假设SpringCloudMongoDBConfiguration
不位于任何超级包中com.foo.model.project
。对此有两种解决方案:
- 坚持将应用程序配置类放入应用程序的根包中的便利性。这将导致您的应用程序包被扫描以查找域类、获取的元数据以及按预期进行的新检测工作。
- 通过覆盖手动将包含域类的包交给基础设施
MongoConfiguration.getMappingBasePackage()
.
您可能会看到配置在本地环境中运行的原因是,映射元数据可能是通过非持久性操作(例如查询)以及从那里进行的其他操作来获取的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)