LoginSignup
0
0

More than 3 years have passed since last update.

org.springframework.data.jpa.repository.JpaRepositoryインタフェースを実作するクラスはなに?

Posted at

僕のサンプルアプリケーション(Spring Data JPAバージョン)を見て、リポジトリにただ2つのインタフェースが作られた

interface.png

インタフェースを実作するクラスは見えない、実際にinfo.saladlam.example.spring.noticeboard.service.MessageServiceImplに注入するインスタンスクラスは何?

MessageServiceImplに注入するインスタンスはプロキシであり、そのターゲットインスタンスクラスはorg.springframework.data.jpa.repository.support.SimpleJpaRepositoryだ。いろいろインターセプターがこのプロキシに付け、その中に大切なインターセプターはorg.springframework.data.repository.core.support.RepositoryFactorySupport.QueryExecutorMethodInterceptorであり。

下のはインタフェースinfo.saladlam.example.spring.noticeboard.repository.MessageRepository

public interface MessageRepository extends JpaRepository<Message, Long> {

    @Query("SELECT m FROM Message m WHERE m.approvedBy IS NOT NULL AND m.publishDate <= :at AND (m.removeDate IS NULL OR m.removeDate > :at) ORDER BY m.publishDate DESC")
    List<Message> findPublished(@Param("at") LocalDateTime at);

    @Query("SELECT m FROM Message m WHERE m.approvedBy IS NULL ORDER BY m.publishDate DESC")
    List<Message> findWaitingApprove();

    @Query("SELECT m FROM Message m WHERE m.owner = :owner ORDER BY m.publishDate DESC")
    List<Message> findByOwner(@Param("owner") String owner);

}

そして親インタフェースorg.springframework.data.jpa.repository.JpaRepository

@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {

    List<T> findAll();
    List<T> findAll(Sort sort);
    List<T> findAllById(Iterable<ID> ids);
    <S extends T> List<S> saveAll(Iterable<S> entities);
    void flush();
    <S extends T> S saveAndFlush(S entity);
    void deleteInBatch(Iterable<T> entities);
    void deleteAllInBatch();
    T getOne(ID id);

    @Override
    <S extends T> List<S> findAll(Example<S> example);

    @Override
    <S extends T> List<S> findAll(Example<S> example, Sort sort);

}

例えば、JpaRepositoryで定義するfindAll()メソッドが呼び出される時、実際にSimpleJpaRepositoryfindAll()メソッドが呼び出される。MessageRepositoryで定義するfindWaitingApprove()メソッドが呼び出される時、インターセプターorg.springframework.data.repository.core.support.RepositoryFactorySupport.QueryExecutorMethodInterceptorが作動させる。下のはorg.springframework.data.repository.core.support.RepositoryFactorySupport.QueryExecutorMethodInterceptor.doInvoke()メソッド

        @Nullable
        private Object doInvoke(MethodInvocation invocation) throws Throwable {

            Method method = invocation.getMethod();
            Object[] arguments = invocation.getArguments();

            if (hasQueryFor(method)) {
                return queries.get(method).execute(arguments);
            }

            return invocation.proceed();
        }

queriesはマップであり、org.springframework.data.jpa.repository.query.JpaQueryMethodインスタンスを蓄え、キーはMessageRepositoryで定義するメソッド。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0