2017-01-12 27 views
5

Pracuję nad aplikacją Spring Boot v1.4.2.RELEASE z JPA.Zależności niektórych ziaren w kontekście aplikacji od cyklu

I określonych interfejsów repozytorium implementacje

ARepository

@Repository 
public interface ARepository extends CrudRepository<A, String>, ARepositoryCustom, JpaSpecificationExecutor<A> { 
} 

ARepositoryCustom

@Repository 
public interface ARepositoryCustom { 
    Page<A> findA(findAForm form, Pageable pageable); 
} 

ARepositoryImpl

@Repository 
public class ARepositoryImpl implements ARepositoryCustom { 
    @Autowired 
    private ARepository aRepository; 
    @Override 
    public Page<A> findA(findAForm form, Pageable pageable) { 
     return aRepository.findAll(
       where(ASpecs.codeLike(form.getCode())) 
       .and(ASpecs.labelLike(form.getLabel())) 
       .and(ASpecs.isActive()), 
       pageable); 
    } 
} 

A usługa AServiceImpl

@Service 
public class AServiceImpl implements AService { 
    private ARepository aRepository; 
    public AServiceImpl(ARepository aRepository) { 
     super(); 
     this.aRepository = aRepository; 
    } 
    ... 
} 

Moja aplikacja nie uruchomi się z komunikatem:

 
*************************** 
APPLICATION FAILED TO START 
*************************** 

Description: 

The dependencies of some of the beans in the application context form a cycle: 

| aRepositoryImpl 
└─────┘ 

Śledziłem wszystkie kroki opisane tutaj w http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behaviour

Proszę o pomoc !

Laurent

+2

Spróbuj usunąć adnotację '@ Repository' od' ARepositoryCustom' –

+0

Nie ma potrzeby, aby oznaczyć jako '' @ ARepositoryCustom' Repository' ponieważ chcemy, aby zapewnić, że to realizacja zamiast generować go z wiosennej Danych – Aeteros

+0

masz interfejs ' ARepository, które rozszerza 'ARepositoryCustom' w implementacji, używasz interfejsu' ARepository'. Wątpię, aby SPring Data Jpa mógł wymyślić pewne rzeczy. Także imho ta metoda należy do twojej usługi, a nie do twojego repozytorium. –

Odpowiedz

4

Jest prosty fix dla oryginalnego problemu: Wystarczy usunąć @Repository z ARepositoryCustom iz ARepositoryImpl. Zachowaj wszystkie nazwy i hierarchie interfejsów/klas. Wszystkie są w porządku.

1

Przetestowałem kod źródłowy i znalazłem coś trudne.

Po pierwsze, z kodem źródłowym, mam następujący błąd:

There is a circular dependency between 1 beans in the application context: 
- ARepositoryImpl (field private test.ARepository test.ARepositoryImpl.aRepository) 
- aRepositoryImpl 

Następnie, myślę Wiosna 'mylić' między ARepository repozytorium (JPA) i ARepositoryImpl (Własne repozytorium). Proponuję Ci zmienić nazwę na pod inną nazwąARepository na coś innego, na przykład BRepository. Zadziałało, gdy zmieniłem nazwę klasy.

Według offcial dokumentacji Spring dane (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/):

These classes need to follow the naming convention of appending the namespace element’s attribute repository-impl-postfix to the found repository interface name. This postfix defaults to Impl