简体   繁体   English

如何在 @Scope 和 @Collection 注释中传递来自 application.properties 的值?

[英]How can I pass a value from application.properties in @Scope and @Collection annotations?

I want to prepare for a migration from Couchbase 6.X to 7.X.我想准备从 Couchbase 6.X 迁移到 7.X。 For this, I want to configure the scope and collection for my cluster.为此,我想为我的集群配置范围和集合。 In the documentation of spring-data-couchbase, I saw that I just need to add @Scope and @Collection on my repository.在 spring-data-couchbase 的文档中,我看到我只需要在我的存储库中添加 @Scope 和 @Collection 。 This configuration works:此配置有效:

package xxx.couchbase;

import xxx.MyDocument;
import org.springframework.data.couchbase.repository.Collection;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
import org.springframework.data.couchbase.repository.Scope;
import org.springframework.stereotype.Repository;

@Repository
@Scope("_default")
@Collection("_default")
public interface MyDocumentRepository extends CouchbaseRepository<MyDocument, String> {
}

What I want to do:我想做的事:

package xxx.couchbase;

import xxx.MyDocument;
import org.springframework.data.couchbase.repository.Collection;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
import org.springframework.data.couchbase.repository.Scope;
import org.springframework.stereotype.Repository;

@Repository
@Scope("${couchbase.scope}")
@Collection("${couchbase.collection}")
public interface MyDocumentRepository extends CouchbaseRepository<MyDocument, String> {
}

application.properties:应用程序.properties:

couchbase.scope=_default
couchbase.collection=_default

This latter configuration doesn't work, because the string value in the annotation is not parsed and is taken literally.后一种配置不起作用,因为注释中的字符串值未被解析,而是按字面意思获取。 I'm working with Spring Boot version 2.7.3, spring-boot-starter-data-couchbase version 2.7.3.我正在使用 Spring Boot 版本 2.7.3,spring-boot-starter-data-couchbase 版本 2.7.3。

Is there another way to pass the value from the application.properties to the annotations?还有另一种方法可以将值从 application.properties 传递给注释吗?

I don't see a way for spring boot to populate arguments of an annotation with values from application.properties.我看不到 spring boot 可以使用 application.properties 中的值填充注释参数的方法。

But you can populate variables from values in application.properties like this:但是您可以从 application.properties 中的值填充变量,如下所示:

@Value("${scopeName}")
private String scopeName;

@Value("${collectionName}")
private String collectionName;

And then define your repository to extend DynamicProxyable然后定义您的存储库以扩展 DynamicProxyable

public interface AirportRepository extends CouchbaseRepository<Airport, String>, DynamicProxyable<AirportRepository>{...}

And then get a repository object with that scope and collection as the defaults:然后获取具有该范围和集合作为默认值的存储库对象:

List<Airport> airports = airportRepository.withScope(scopeName).withCollection(collectionName).findByName(iata);

see https://docs.spring.io/spring-data/couchbase/docs/4.4.6/reference/html/#couchbase.collections请参阅https://docs.spring.io/spring-data/couchbase/docs/4.4.6/reference/html/#couchbase.collections

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM