No qualifying bean of type available (Spring Data)
This is caused by the fact that you defined the bean as StockService
and you are referring to it as stockService
, this should be the same name, case sensitive in both the service and the controller.
So the bean definition should be updated from:
@Service("StockService")
To :
@Service("stockService")
Because you are referring to it with stockService
, in the controller in:
@Autowired
private StockService stockService;
Note:
Also make sure that your bean is defined in the scanned packages of spring.
Nesquik27
Updated on June 28, 2022Comments
-
Nesquik27 almost 2 years
I'm getting this error when I try to autowire interface that extends CrudRepository. I have two hibernate xml configs for 2 databases. The full stack is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloController': Unsatisfied dependency expressed through field 'stockService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.publishing.test.stock.services.StockService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
<hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">org.postgresql.Driver</property> <property name="connection.url"></property> <property name="connection.username"></property> <property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">100</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.PostgreSQL95Dialect</property> <!-- Disable the second-level cache --> <!--<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>--> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop the existing table and create new one --> <property name="hbm2ddl.auto">update</property> <property name="packagesToScan">com.publishing</property> <!-- Mention here all the model classes --> <mapping class="com.publishing.models.Stock"/> </session-factory>
@Controller public class HelloController { @Autowired private StockService stockService;
I have 3 lines also in Spring Config
<context:component-scan base-package="com.publishing" /> <context:annotation-config /> <jpa:repositories base-package="com.publishing" />
Service is
@Service("StockService") public interface StockService extends CrudRepository<Stock, Long> {
Edit:
Ok, now we have edited hibernate.cfg.xml
<!-- Drop the existing table and create new one --> <property name="hbm2ddl.auto">update</property> <!--<property name="packagesToScan">com.publishing</property>--> <!-- Mention here all the model classes --> <mapping class="com.publishing.models.Stock"/>
And service
@Service("stockService") public interface StockService extends CrudRepository<Stock, Long> {