具有自定义集合名称的Spring Data MongoDB存储库
我正在使用Spring Data for MongoDB,我需要能够在运行时配置集合。
我的存储库定义为:
@Repository public interface EventDataRepository extends MongoRepository { }
我试过这个愚蠢的例子:
@Document(collection = "${mongo.event.collection}") public class EventData implements Serializable {
但是mongo.event.collection没有像使用@Value注释那样解析为名称。
多一点调试和搜索,我尝试了以下内容:@Document(collection =“#{$ {mongo.event.collection}}”)
这产生了一个例外:
Caused by: org.springframework.expression.spel.SpelParseException: EL1041E:(pos 1): After parsing a valid expression, there is still more data in the expression: 'lcurly({)' at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:129) at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:60) at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:32) at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpressions(TemplateAwareExpressionParser.java:154) at org.springframework.expression.common.TemplateAwareExpressionParser.parseTemplate(TemplateAwareExpressionParser.java:85)
也许我只是不知道如何使用SPel来访问Spring的Property Configurer中的值。
单步执行代码时,我发现有一种方法可以指定集合名称甚至表达式,但是,我不确定应该将哪个注释用于此目的或如何执行。
谢谢。 -AP_
所以,最后,这是一个解决问题的方法。 我想我真的不知道如何使用SPeL表达式从Spring Properties Configurer访问数据。
在我的@Configuration类中:
@Value("${mongo.event.collection}") private String mongoEventCollectionName; @Bean public String mongoEventCollectionName() { return mongoEventCollectionName; }
在我的文件上:
@Document(collection = "#{mongoEventCollectionName}")
这似乎可以正常工作并正确选取我的.properties文件中配置的名称,但是,我仍然不确定为什么我不能像在@Value注释中那样使用$来访问该值。
定义你的实体类
@Document(collection = "${EventDataRepository.getCollectionName()}") public class EventData implements Serializable { Define a custom repository interface with getter and setter methods for "collectionName" public interface EventDataRepositoryCustom { String getCollectionName(); void setCollectionName(String collectionName); }
使用“collectionName”实现为自定义存储库提供实现类
public class EventDataRepositoryImpl implements EventDataRepositoryCustom{ private static String collectionName = "myCollection"; @Override public String getCollectionName() { return collectionName; } @Override public void setCollectionName(String collectionName) { this.collectionName = collectionName; } }
将EventDataRepositoryImpl
添加到存储库接口的扩展列表中,看起来就像这样
@Repository public interface EventDataRepository extends MongoRepository, EventDataRepositoryImpl { }
现在在您使用MongoRepository
Service类中设置集合名称,它看起来就像
@Autowired EventDataRepository repository ; repository.setCollectionName("collectionName");
实体类
@Document // remove the parameters from here public class EscalationCase { }
配置类
public class MongoDBConfiguration { private final Logger logger = LoggerFactory.getLogger(MongoDBConfiguration.class); @Value("${sfdc.mongodb.collection}") //taking collection name from properties file private String collectionName; @Bean public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory, MongoMappingContext context) { MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory), context); converter.setTypeMapper(new DefaultMongoTypeMapper(null)); MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter); if (!mongoTemplate.collectionExists(collectionName)) { mongoTemplate.createCollection(collectionName); // adding the collection name here } return mongoTemplate; } }