2016-10-05 17 views
1

SimpleJpaRepositoryを無効にしようとすると、@Autowired経由で他のBeanを追加できません。この場合、豆はどのように注入できますか?@AutowiredはSimpleJpaRepositoryの内線では機能しません

+0

同様の問題が未解決のままします。http:// stackoverf low.com/questions/34528234/how-to-inject-bean-into-own-implementation-simplejparepository – Alec

+1

質問から不必要なコードを編集したのは私でした。私はまたコメントで、余分なコードが問題またはその解決に関係していないことを明確にしました。編集が不要であると感じた場合は、元の質問に戻してください。下記の私の答えをチェックして、ソリューションに到達するために追加のコードをすべて必要としない理由と、提案されたソリューションがあなたのために機能するかどうかを確認してください。 – manish

答えて

2

BaseDAOのインスタンスは、Spring管理のBeanではないため、@Autowiredを介してautowiringすると、すぐには動作しません。したがって、依存関係はBaseDAOインスタンスに注入する必要があります。


ステップ1:これは、リポジトリの作成時にカスタムリポジトリ実装の依存関係をautowireする必要があります春のどこかで入手できApplicationContext

@Component 
class SpringContext implements ApplicationContextAware { 
    private static ApplicationContext CONTEXT; 

    public void setApplicationContext(final ApplicationContext context) throws BeansException { 
    CONTEXT = context; 
    } 

    public static ApplicationContext getContext() { return CONTEXT; } 
} 

を持っています。

ステップ2JpaRepositoryFactoryBean

class ExtendedJPARepositoryFactoryBean<R extends JpaRepository<T, ID>, T, ID extends Serializable> 
     extends JpaRepositoryFactoryBean<R, T, ID> { 
    private static class ExtendedJPARepositoryFactory<T, ID extends Serializable> extends JpaRepositoryFactory { 
    public ExtendedJPARepositoryFactory(final EntityManager entityManager) { 
     super(entityManager); 
    } 

    protected Class<?> getRepositoryBaseClass(final RepositoryMetadata metadata) { 
     return isQueryDslExecutor(metadata.getRepositoryInterface()) 
      ? QueryDSLJPARepository.class 
      // Let your implementation be used instead of SimpleJpaRepository. 
      : BaseDAO.class; 
    } 

    protected <T, ID extends Serializable> SimpleJpaRepository<?, ?> getTargetRepository(
     RepositoryInformation information, EntityManager entityManager) { 
     // Let the base class create the repository. 
     final SimpleJpaRepository<?, ?> repository = super.getTargetRepository(information, entityManager); 

     // Autowire dependencies, as needed. 
     SpringContext.getContext() 
        .getAutowireCapableBeanFactory() 
        .autowireBean(repository); 

     // Return the fully set up repository. 
     return repository; 
    } 
    } 

    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { 
    return new ExtendedJPARepositoryFactory(entityManager); 
    } 
} 
介しAutowire依存: SimpleJpaRepository

class BaseDAO<T, ID extends Serializable> 
     extends SimpleJpaRepository<T, ID> { 
    @Autowired 
    private Dependency dependency; 
} 

ステップ3を拡張0

手順4a:設定春データJPAはあなたのファクトリーBeanを使用する(Java設定:設定春データJPAはあなたの工場の豆(XML構成)

<jpa:repositories base-package="org.example.jpa" 
        factory-class="org.example.jpa.ExtendedJPARepositoryFactoryBean"/> 

ステップ4bを

を使用するには)

@EnableJpaRepositories(repositoryFactoryBeanClass = ExtendedJPARepositoryFactoryBean.class) 
+0

実際に私はこのソリューションについて考えていましたが、なぜ春のデータ作成者が考慮しなかったのか、追加の動作が必要になるのではないかと疑問に思っていました。ソリューションは完璧に機能しますが、まだ回避策のように見えます。とにかくそれを受け入れる – Alec

-1

DAOの中に何かを注入する必要があることをSpringに知らせるためには、@Componentで注釈を付ける必要があります。

詳しくは、http://docs.spring.io/autorepo/docs/spring-boot/current/reference/html/using-boot-spring-beans-and-dependency-injection.htmlをご覧ください。

+0

はい、私は基本を知っています:)しかし、この特定のケースでは、@Componentで注釈が付けられている場合 - 私はすぐに取得します com.example.model.dao.BaseDAOのコンストラクタのパラメータ0には、 'org.springframework .data.jpa.repository.support.JpaEntityInformation 'が見つかりませんでした。 アプリが起動しない、明らかに – Alec

関連する問題