簡體   English   中英

沒有合格的bean類型(Spring Data)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM