@EnableMongo在Cloud Foundry/mongolab上审核MongoDB [英] @EnableMongoAuditing for MongoDB on Cloud Foundry / mongolab

查看:123
本文介绍了@EnableMongo在Cloud Foundry/mongolab上审核MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置在我的本地系统上有效,但是当我将其部署到CloudFoundry/mongolab上时无效.

My setup works on my local but not when I deploy it to CloudFoundry/mongolab.

该配置与文档.

我的本​​地spring配置

My local spring config

@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的设置

This is the cloud foundry setup

@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中保存文档时遇到的错误是:

And the error I'm getting when I try to save a document in Cloud Foundry is:

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)

有什么想法吗?是我的配置文件等吗? 提前致谢 尼古拉斯

Any ideas? Is it my config file etc..? Thanks in advance Niclas

推荐答案

通常是由于为实体获取的Mongo映射元数据在应用程序启动时未扫描实体而导致的.默认情况下,AbstractMongoConfiguration使用实际配置类的包在启动时查找带@Document注释的类.

This is usually caused if the Mongo mapping metadata obtained for entities does not scan entities at application startup. By default, AbstractMongoConfiguration uses the package of the actual configuration class to look for @Document annotated classes at startup.

该异常消息使我假设SpringCloudMongoDBConfiguration不在com.foo.model.project的任何超级包中.有两种解决方案:

The exception message makes me assume that SpringCloudMongoDBConfiguration is not located in any of the super packages of com.foo.model.project. There are two solutions to this:

  • 请坚持将应用程序配置类放入应用程序的根包中,以方便使用.这将导致您的应用程序包被扫描以检查域类,获取的元数据以及按预期进行的is-new-detecting工作.
  • 通过覆盖MongoConfiguration.getMappingBasePackage()手动将包含域类的包移交给基础结构.
  • Stick to the convenience of putting application configuration classes into the root package of your application. This will cause your application packages be scanned for domain classes, metadata obtained, and the is-new-detection work as expected.
  • Manually hand the package containing domain classes to the infrastructure by overriding MongoConfiguration.getMappingBasePackage().

您可能会看到配置在本地环境中工作的原因是,映射元数据可能是通过非持久性持久性操作(例如查询)获得的,其他所有操作都从那里进行.

The reason you might see the configuration working in the local environment is that the mapping metadata might be obtained through a non-persisting persistence operation (e.g. a query) and everything else proceeding from there.

这篇关于@EnableMongo在Cloud Foundry/mongolab上审核MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆