接続の設定
Oracle NoSQL Database SDK for Spring Dataに接続とセキュリティのパラメータを公開するには、AbstractNosqlConfiguration
クラスの拡張クラスを作成する必要があります。このコードは、必要に応じてカスタマイズできます。次のステップを実行して、Oracle NoSQL Databaseへの接続を設定します。
ステップ1: アプリケーションで、NosqlDbConfig
クラスを作成します。このクラスには、Oracle NoSQL Databaseプロキシへの接続の詳細を含めます。このNosqlDbConfig
クラスに、@Configuration
と@EnableNoSQLRepositories
の注釈を指定します。@Configuration
注釈では、@Configuration
注釈が付いたクラスが、プログラムの実行前にロードする必要がある構成クラスであることをSpring Data Frameworkに通知します。@EnableNoSQLRepositories
注釈では、NosqlRepository
インタフェースを拡張するリポジトリのプログラムと参照をロードする必要があることをSpring Data Frameworkに通知します。@Bean
注釈は、リポジトリをインスタンス化するために必要です。
ステップ2: NosqlDBConfig
クラスのインスタンスを返す@Bean
の注釈が付いたメソッドを作成します。NosqlDBConfig
クラスは、Oracle NoSQL Databaseを認証するためにSpring Data Frameworkでも使用されます。
ステップ3: NosqlDbConfig
クラスをインスタンス化します。NosqlDbConfig
クラスをインスタンス化すると、Spring Data FrameworkはOracle NoSQL Databaseでの認証によって内部的にOracle NoSQL Databaseハンドルをインスタンス化します。
ノート:
認証の失敗時にスローされる可能性のある接続エラーを捕捉するために、例外コード・ブロックを追加することもできます。ノート:
前述のステップを使用してOracle NoSQL Databaseハンドルを作成する場合には制限があります。その制限とは、アプリケーションが同時に複数の異なるクラスタにできなくなることです。これは、Spring Data Frameworkの制限です。Spring Data Frameworkの詳細は、「Spring Core」を参照してください。ノート:
SpringアプリケーションからOracle NoSQL Databaseに接続できない場合は、例外ブロックを追加して、デバッグのためのメッセージを出力できます。次の例に示すように、StoreAccessTokenProvider
クラスを使用すると、Oracle NoSQL Databaseに接続して認証するようにSpring Data Frameworkを構成できます。セキュアでないアクセス権のあるOracle NoSQL DatabaseプロキシのURLを指定する必要があります。
/*Annotation to specify that this class can be used by the
Spring Data Framework as a source of bean definitions.*/
@Configuration
//Annotation to enable NoSQL repositories.
@EnableNosqlRepositories
public class AppConfig extends AbstractNosqlConfiguration {
/*Annotation to tell the Spring Data Framework that the returned object
should be registered as a bean in the Spring application.*/
@Bean
public NosqlDbConfig nosqlDbConfig() {
AuthorizationProvider authorizationProvider;
authorizationProvider = new StoreAccessTokenProvider();
//Provide the host name and port number of the NoSQL cluster.
return new NosqlDbConfig("http://<host:port>", authorizationProvider);
}
}
次の例では、前述の例を変更してセキュアなOracle NoSQL Databaseストアに接続します。StoreAccessTokenProvider
クラスの詳細は、Java SDK APIリファレンスのStoreAccessTokenProviderに関する項を参照してください。
/*Annotation to specify that this class can be used by the
Spring Data Framework as a source of bean definitions.*/
@Configuration
//Annotation to enable NoSQL repositories.
@EnableNosqlRepositories
public class AppConfig extends AbstractNosqlConfiguration {
/*Annotation to tell the Spring Data Framework that the returned object
should be registered as a bean in the Spring application.*/
@Bean
public NosqlDbConfig nosqlDbConfig() {
AuthorizationProvider authorizationProvider;
//Provide the username and password of the NoSQL cluster.
authorizationProvider = new StoreAccessTokenProvider(user, password);
//Provide the host name and port number of the NoSQL cluster.
return new NosqlDbConfig("http://<host:port>", authorizationProvider);
}
}
StoreAccessTokenProvider
のパラメータ化されたコンストラクタは次の引数を使用します。
username
は、kvstoreのユーザー名です。password
は、kvstoreユーザーのパスワードです。
セキュリティ構成の詳細は、『管理者ガイド』のNoSQLハンドルの作成に関する項を参照してください。
次の例に示すように、SignatureProvider
クラスを使用して、Oracle NoSQL Database Cloud Serviceに接続して認証するようにSpring Data Frameworkを構成できます。Java SDK APIリファレンスのSignatureProviderに関する項を参照してください。
/*Annotation to specify that this class can be used by the
Spring Data Framework as a source of bean definitions.*/
@Configuration
//Annotation to enable NoSQL repositories.
@EnableNosqlRepositories
public class AppConfig extends AbstractNosqlConfiguration {
/*Annotation to tell the Spring Data Framework that the returned object
should be registered as a bean in the Spring application.*/
@Bean
public NosqlDbConfig nosqlDbConfig() {
SignatureProvider signatureProvider;
/*Details that are required to authenticate and authorize access to
the Oracle NoSQL Database Cloud Service are provided.*/
signatureProvider = new SignatureProvider(
<tenantId>, //The Oracle Cloud Identifier (OCID) of the tenancy.
<userId>, //The Oracle Cloud Identifier (OCID) of a user in the tenancy.
<fingerprint>, //The fingerprint of the key pair used for signing.
<privateKeyFile>, //Full path to the key file.
<passphrase> //Optional. A pass phrase for the key, if it is encrypted.
);
/*Provide the service URL of the Oracle NoSQL Database Cloud Service and
update the 'Region.US_PHOENIX_1' with an appropriate value.*/
return new NosqlDbConfig(Region.US_PHOENIX_1,signatureProvider);
}
}