Раньше я часто использовал MyBatis, как показано ниже.
Код: Выделить всё
// I often declare mapper first, and inject it to my persistence.
@Mapper
public interface MyBatisMapper {/* ... */}
/* ------------ */
@Repository
public class MyBatisRepo {
private final MyBatisMapper mapper;
public MyBatisRepo(MyBatisMapper mapper) {
this.mapper = mapper;
}
/* ... */
}
Вот код JPA< /p>
Код: Выделить всё
TestEntity
Код: Выделить всё
// entity just for test
@Entity
public class TestEntity {
@Id
private Long id;
}
- Интерфейс репозитория (контракты)
Код: Выделить всё
// Persistence contracts
public interface TestRepo {
}
Код: Выделить всё
JPA DAO
Код: Выделить всё
//
public interface JPATestRepo extends JpaRepository {
}
- Реализует репозиторий
Код: Выделить всё
@Repository
public class JPATestRepoImpl implements TestRepo {
private final JPATestRepo jpaRepo;
public JPATestRepoImpl(JPATestRepo jpaRepo) {
this.jpaRepo = jpaRepo;
}
}
- Ошибки
Код: Выделить всё
2024-10-19T19:46:00.760+09:00 WARN 66384 --- [testing] [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'JPATestRepoImpl' defined in file [/~~~/Desktop/Coding/testing/build/classes/java/main/core/testing/JPATestRepoImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'JPATestRepoImpl': Requested bean is currently in creation: Is there an unresolvable circular reference?
2024-10-19T19:46:00.761+09:00 INFO 66384 --- [testing] [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2024-10-19T19:46:00.762+09:00 INFO 66384 --- [testing] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2024-10-19T19:46:00.807+09:00 INFO 66384 --- [testing] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2024-10-19T19:46:00.811+09:00 INFO 66384 --- [testing] [ main] .s.b.a.l.ConditionEvaluationReportLogger :
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-10-19T19:46:00.825+09:00 ERROR 66384 --- [testing] [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌──->──┐
| JPATestRepoImpl defined in file [/~~~/Desktop/Coding/testing/build/classes/java/main/core/testing/JPATestRepoImpl.class]
└── выше, это не так. (по крайней мере, на мой взгляд)
Я трижды перезапускал, удалял и пересобирал приложение, но все равно.
[b]ОДНАКО ,[/b] если я переименую JPATestRepoImpl в другое (например, TestRepoJPAImpl), [b]циклическая зависимость исчезнет, как по волшебству...[/b]
Похоже, что Spring или JPA конфликтуют имена своих компонентов друг с другом, но я понятия не имею, почему это происходит.
Поэтому я хотел спросить, действительно ли в моем файле существует циклическая зависимость исходный код или его происхождение, если его нет. Если существует какая-либо политика, исходящая от JPA, есть ли по ней какая-либо документация?
Вы можете увидеть полный исходный код здесь
Подробнее здесь: [url]https://stackoverflow.com/questions/79105118/circular-dependency-while-injecting-jpa-repository-to-implementation[/url]