Reputation: 287760
I have an entity class called Screenshot
and and a repository declared as this:
public interface ScreenshotRepository extends JpaRepository<Screenshot, UUID>, ScreenshotRepositoryCustom
The custom repository is defined like this:
interface ScreenshotRepositoryCustom
and
class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {
private final ScreenshotRepository screenshotRepo;
@Autowired
public ScreenshotRepositoryImpl(ScreenshotRepository screenshotRepo) {
this.screenshotRepo = screenshotRepo;
}
This is following what's described in this other Stack Overflow question: How to add custom method to Spring Data JPA
Now, IntelliJ is giving me a warning:
Autowired members must be defined in a valid Spring bean
I tried adding these annotations to ScreenshotRepositoryImpl
but none of the worked:
@Repository
@Component
@Service
but none work. Obviously some are wrong but I was experimenting. What's the correct annotation.
With @Repository
, I get this error:
2017-11-23 12:30:04.064 WARN 20576 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'screenshotsController' defined in file [C:\Users\pupeno\Documents\Dashman\java\dashmanserver\out\production\classes\tech\dashman\server\controllers\ScreenshotsController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'screenshotRepositoryImpl' defined in file [C:\Users\pupeno\Documents\Dashman\java\dashmanserver\out\production\classes\tech\dashman\server\models\ScreenshotRepositoryImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'screenshotRepositoryImpl': Requested bean is currently in creation: Is there an unresolvable circular reference?
2017-11-23 12:30:04.064 INFO 20576 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-11-23 12:30:04.064 INFO 20576 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2017-11-23 12:30:04.080 INFO 20576 --- [ restartedMain] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-11-23 12:30:04.080 ERROR 20576 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
screenshotsController defined in file [C:\Users\pupeno\Documents\Dashman\java\dashmanserver\out\production\classes\tech\dashman\server\controllers\ScreenshotsController.class]
┌─────┐
| screenshotRepositoryImpl defined in file [C:\Users\pupeno\Documents\Dashman\java\dashmanserver\out\production\classes\tech\dashman\server\models\ScreenshotRepositoryImpl.class]
└─────┘
Upvotes: 0
Views: 1247
Reputation: 3797
Your dependencies form a cycle: ScreenshotRepository
extends ScreenshotRepositoryCustom
, but the ScreenshotRepositoryCustom
implementation depends on ScreenshotRepository
. Because of this cycle, none of the beans can complete their instantiation.
Spring Data does not support injection via constructor in these scenarios. Attempting to do so will result in a dependency cycle error. To be able to inject ScreenshotRepository
into ScreenShotRepositoryImpl
, you'd need to do injection via field:
@Repository
public class ScreenshotRepositoryImpl implements ScreenshotRepositoryCustom {
@Autowired
ScreenshotRepository screenshotRepository ;
...
}
Upvotes: 2