2017-07-04 5 views
0

すべてのSpringデータリポジトリに共通の動作を提供するために、独自のカスタムSpringデータ共通リポジトリがあります。リポジトリの作成時にEntityManagerを変更するだけです。しかし、JpaRepositoryFactoryBeanにSpring Beanを挿入することはできません。なぜなら、Beanは新しい演算子を使用して作成されるからです。JpaRepositoryFactoryBeanにSpring Beanを挿入する方法

public class BasicJpaRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends JpaRepositoryFactoryBean<T, S, ID> { 

@Autowired 
private SomeService service; // - it does not work 

    @Override 
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager em) { 
    // do some logic here 
    service.doSmth(); 
    return new CommonRepositoryFactory<>(em); 
    } 

    private static class CommonRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory { 

    private final EntityManager em; 

    public CommonRepositoryFactory(EntityManager em) { 
     super(em); 
     this.em = em; 
    } 

    @SuppressWarnings("unchecked") 
    protected Object getTargetRepository(RepositoryMetadata metadata) { 
     JpaEntityInformation entityInformation = getEntityInformation(metadata.getDomainType()); 
     return new CommonRepositoryImpl(entityInformation, em); 
    } 

    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { 
     return CommonRepositoryImpl.class; 
    } 
    } 

} 
+0

あなたはどの春バージョンを使用していますか? – ledniov

+0

.. 'JpaRepositoryFactoryBean'は' new'経由で作成されていますか? (それは避けられないのですか?) – xerx593

+0

実際に私は春のブート1.4を使用しているので、春のバージョンは4.3.2 – idmitriev

答えて

0

そのクラスまたはそのクラスから継承するセッターを実装します。

+0

Beanをこの工場に設定するにはどうすればいいですか? ?それは制御不能です。 – idmitriev

+0

このクラスを拡張し、拡張子にセッターを含めます – Andres

関連する問題