簡體   English   中英

具有自定義集合名稱的Spring Data MongoDB存儲庫

[英]Spring Data MongoDB Repository with custom collection name

我正在使用Spring Data for MongoDB,我需要能夠在運行時配置集合。

我的存儲庫定義為:

@Repository
public interface EventDataRepository extends MongoRepository<EventData, String> {
}

我試過這個愚蠢的例子:

@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 {

使用“collectionName”的getter和setter方法定義自定義存儲庫接口

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<EventData, String>, EventDataRepositoryImpl  {
}

現在在您使用MongoRepository Service類中設置集合名稱,它看起來像

@Autowired
EventDataRepository  repository ;

repository.setCollectionName("collectionName");

您可以通過使用SPeL來解決此問題:

@Document(collection = "#{environment.getProperty('mongo.event.collection')}")
public class EventData implements Serializable {
    ...
}

更新Spring 5.x:

從Spring 5.x開始,您需要額外的@ before 環境

@Document(collection = "#{@environment.getProperty('mongo.event.collection')}")
public class EventData implements Serializable {
    ...
}

實體類

@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;

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM