SimpleJpaRepository
を無効にしようとすると、@Autowired
経由で他のBeanを追加できません。この場合、豆はどのように注入できますか?@AutowiredはSimpleJpaRepositoryの内線では機能しません
答えて
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; }
}
を持っています。
ステップ2:
JpaRepositoryFactoryBean
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)
実際に私はこのソリューションについて考えていましたが、なぜ春のデータ作成者が考慮しなかったのか、追加の動作が必要になるのではないかと疑問に思っていました。ソリューションは完璧に機能しますが、まだ回避策のように見えます。とにかくそれを受け入れる – Alec
DAOの中に何かを注入する必要があることをSpringに知らせるためには、@Componentで注釈を付ける必要があります。
はい、私は基本を知っています:)しかし、この特定のケースでは、@Componentで注釈が付けられている場合 - 私はすぐに取得します com.example.model.dao.BaseDAOのコンストラクタのパラメータ0には、 'org.springframework .data.jpa.repository.support.JpaEntityInformation 'が見つかりませんでした。 アプリが起動しない、明らかに – Alec
- 1. Spring @Autowiredは新しいスレッドでは機能しません
- 2. @AutowiredアノテーションはSpringスタンドアロンアプリケーションでは機能しません
- 3. @Autowiredは実行可能ファイル内では動作しません
- 4. Springアノテーション@Autowiredを使用したコンストラクタインジェクションは機能しません
- 5. @autowiredを使用したSpringインジェクションは機能しません。
- 6. 私の便利なinitは内線では機能しません
- 7. Springデータautowired db接続がJUnitテストでは機能しません
- 8. 春@Autowiredと@Valueプロパティが機能しません
- 9. アンドロイドリストビューの垂直分割線は機能しません
- 10. LibクリップボードはPromiseの内部では機能しません
- 11. JQuery .widthは私のifステートメント内では機能しません
- 12. android-OnClickListenerはリサイクルアダプター内のビューでは機能しません
- 13. RelativeSourceへのバインドTemplatedParentはControlTemplate内では機能しません
- 14. OpenGL法線は機能しませんか?
- 15. 直線勾配のcalcはIE/Edgeでは機能しません
- 16. @ Html.ValidationSummary()はAjax.BeginForm内では機能しません
- 17. $(this)はonclick属性内では機能しません
- 18. ImageButtonイベントリスナーはScrollView - > HorizontalScrollView内では機能しません
- 19. ng-clickはag-gridセル内では機能しません
- 20. colResizableプラグインはjQueryタブ内では機能しません
- 21. str.replaceは関数内では機能しません
- 22. withTimeoutは関数内では機能しませんか?
- 23. プッシュ機能は内部では動作しません
- 24. GoogleマップはjQuery UIタブ内では機能しません
- 25. ボタンはフラグメント内では機能しません
- 26. cdコマンドはbashスクリプト内では機能しません
- 27. onClickはフラグメント内では機能しません
- 28. MapViewはScrollView内では機能しません - Appcelerator
- 29. nth-of-typeはリスト内では機能しません
- 30. DatatablesはSys.Application.add_load内では機能しません
同様の問題が未解決のままします。http:// stackoverf low.com/questions/34528234/how-to-inject-bean-into-own-implementation-simplejparepository – Alec
質問から不必要なコードを編集したのは私でした。私はまたコメントで、余分なコードが問題またはその解決に関係していないことを明確にしました。編集が不要であると感じた場合は、元の質問に戻してください。下記の私の答えをチェックして、ソリューションに到達するために追加のコードをすべて必要としない理由と、提案されたソリューションがあなたのために機能するかどうかを確認してください。 – manish