接続の設定

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 FrameworkOracle 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);
    }
}