0
votes

My project is setup as below.

enter image description here

As the diagram depicts, I have a helper project (with 2 @Autowired clocks), and my main project with 1 @Autowired clock. All the three clocks mean the same object.

When I start up the application, I get the error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helper2': Unsatisfied dependency expressed through field 'clock'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.time.Clock' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)

.

.

.

.

~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.time.Clock' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]

2
How do you provide your beans definition? Through .xml file? As I see you have MainConfig.class where the @Bean is defined, but in that case, that class needs to be marked as @Configuration. - DevDio
In Main Config, you are defining bean of type clock and autowiring as a class member. Why do you need @autowired clock there? - Himanshu Bhardwaj
You have a circular dependency. - chrylis -cautiouslyoptimistic-
Thankyou Devdio. I am not using xml file. I have marked my MainConfig with @Configuration, but I get the same error. - Deepboy
Thankyou for your response Himanshu. I have removed the Autowired from the MainConfig. The MainConfig has only the method, @Bean public Clock getClock() { return Clock.systemDefaultZone(); } Helper2 and Helper3 have the Autowired Clock clock annotation. Still the same error though. - Deepboy

2 Answers

0
votes

Spring expects a Bean definition of an implementation of java.time.Clock in order to be able to autowire it. Since you already expose the return value of Clock.systemDefaultZone as a @Bean, the only thing you have to do is add @Configuration to your MainConfig.java and make sure MainConfig is properly picked up by Spring, either via @ComponentScan or @Import.

-3
votes

You cannot autowire java.time.Clock since it isn't a Spring component. A spring component is a class that's considered for auto-detection.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Component.html

I was editing my response but it seems like @ptikobj has already answered the question in a good way. I'd add to that, that adding @SpringBootConfiguration to the class instead of just @Configuration will handle the componentscan already, so there will be no need for that.