2014-05-13 32 views
2

我試圖手動定義自定義春數據倉庫,我有以下3類:春數據JPA庫的手動設置,自定義實現

public interface PersonRepository extends JpaRepository<Person, Long>... 

public interface PersonRepositoryCustom 

public class PersonRepositoryImpl implements PersonRepositoryCustom { 
    @Resource 
private PersonRepository personRepository; 
...... 
} 

要在配置類我有以下的配置如下:

@Bean 
public PersonRepositoryImpl personRepositoryImpl() { 
return new PersonRepositoryImpl(); 
} 

@Bean 
public PersonRepository personRepository() { 
    return getFactoryBean(PersonRepository.class, personRepositoryImpl()); 
} 

private <T> T getFactoryBean(Class<T> repository, Object customImplementation) { 
    BaseRepositoryFactoryBean factory = new BaseRepositoryFactoryBean(); 
    factory.setEntityManager(entityManager); 
    factory.setBeanFactory(beanFactory); 
    factory.setRepositoryInterface(repository); 
    if (customImplementation != null) { 
     factory.setCustomImplementation(customImplementation); 
    } 
    factory.afterPropertiesSet(); 
    return (T) factory.getObject(); 

} 

我遇到的問題是,我得到 「錯誤創建名稱爲豆‘personRepository’:當前請求的bean是在創造:是否有一個無法解決的循環引用」

這似乎是由於該PersonRepositoryImpl包含資源參考personRepository接口的事實。

如果我在配置類上使用EnableJpaRepositories,那麼一切似乎工作正常。 但是,我不想使用該註釋,它會根據軟件包進行掃描,並且我希望獲得更精細的可配置性。

因此,沒有人知道如何手動設置彈簧自定義存儲庫,允許注入沒有循環引用問題?

有人嗎?

+0

您可以限制該軟件包掃描的範圍。 – chrylis

回答