[英]No qualifying bean of type available (Spring Data)
当我尝试自动连接扩展CrudRepository的接口时,我收到此错误。 我有2个数据库的两个hibernate xml配置。 完整的堆栈是
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'helloController'的bean时出错:通过字段'stockService'表示的不满意的依赖关系; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有'com.publishing.test.stock.services.StockService'类型的限定bean可用:预计至少有1个bean可以作为autowire候选者。 依赖注释:{@ 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;
我在Spring Config中也有3行
<context:component-scan base-package="com.publishing" />
<context:annotation-config />
<jpa:repositories base-package="com.publishing" />
服务是
@Service("StockService")
public interface StockService extends CrudRepository<Stock, Long> {
编辑:
好的,现在我们编辑了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"/>
和服务
@Service("stockService")
public interface StockService extends CrudRepository<Stock, Long> {
这是因为您将bean定义为StockService
并将其称为stockService
,这应该是相同的名称,在服务和控制器中都区分大小写。
所以bean定义应该从以下更新:
@Service("StockService")
至 :
@Service("stockService")
因为你用stockService
引用它, stockService
在控制器中:
@Autowired
private StockService stockService;
注意:
还要确保您的bean在扫描的spring包中定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.