マジックリポジトリ:Spring Data-JPAとGoogle Guiceの統合

少し前のHabréには、 Spring Data-JPAプロジェクトに特化した良い投稿がありました。

プロジェクトは本当に感銘を受けました。なぜなら 彼は、適切に設計されたリポジトリソリューションを提案しました。

このトピックについては独自の開発がありましたが、Spring Data-JPAははるかにエレガントで機能的なソリューションでした。

落とし穴が1つありました。私が協力しているプロジェクトはGoogle Guiceを使用して構築されました。

かつて、それは私の意識的な選択であり、一般的に、プロジェクトチームと顧客の両方に依然として適しています。



...しかし、Spring Data-JPAリポジトリのコンセプトはあまりにも美味しかった...





いくつかの考えの結果、Spring Data-JPAとguice-persistサブシステムの間に、標準のGoogle Guice統合モジュールのセットから統合モジュールを作成することが決定されました。

Spring Data-JPAのアーキテクチャは適切に設計されているため、この統合が可能です。



それの由来



統合モジュールの主な機能のリスト:





どのように作業するのですか?



ここでは、3つの主要なステップを区別できます。



1. リポジトリオプションを作成します。

public interface AccountRepository extends JpaRepository<Account, Long>,

EntityManagerProvider {



Account findAccountByUuid( String uuid);



@Query( "select a from Account a where a.name = :name" )

Account findAccountByName(@Param( "name" ) String name);

}










2.統合Guiceモジュールのインストール:

install( new JpaRepositoryModule( "my-persistence-unit" ) {

protected void configureRepositories() {

bind(AccountRepository. class ).toProvider( new JpaRepositoryProvider<AccountRepository>());

}

});











3.注入および使用:

public class AccountService {



@Inject

private AccountRepository accountRepository;



public void registerUser( String login, String password) throws RegistrationException{

// ... some checks & etc

accountRepository.save( new Account(login, password));

// ... something else

}



public Account findAccount( String login) throws FinderException{

return accountRepository.findAccountByLogin(login);

}

}










ライブの使用例は、プロジェクトのJUnitテストにあります。

http://code.google.com/p/guice-repository/source/browse/tags/1.0.0/src/test/java/com/google/code/guice/repository/



使用のより詳細な側面は、プロジェクトのドキュメントに記載されています。

http://code.google.com/p/guice-repository/wiki/DevGuide



このプロジェクトは、Apache License 2.0ライセンスで誰でも利用できます。

http://code.google.com/p/guice-repository/



中央のMavenリポジトリのアーティファクト:

< dependency >

< groupId > com.google.code.guice-repository </ groupId >

< artifactId > guice-repository </ artifactId >

< version > 1.0.0 </ version >

</ dependency >










プロジェクトがあなたにとって有用であれば、私はうれしいです:)

ご清聴ありがとうございました!



All Articles