当前位置: 首页 > 知识库问答 >
问题:

没有可用类型的合格bean(Spring数据)

谭坚诚
2023-03-14

当我尝试自动连接扩展CrudRepository的接口时,我遇到了这个错误。我有两个用于两个数据库的hibernate xml配置。整个堆栈是

unsatisfiedDependencyException:创建名为“Hello Controller”的bean时出错:通过字段“stock service”表示的不满足的依赖项;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.publishing.test.stock.services.StockService”类型的合格bean可用:应至少有一个合格的自动候选bean。依赖项批注:{@org.springframework.beans.factory.annotation.AutoWiredanNotationBeanPostProcessor}org.springframework.beans.factory.annotation.inject(AutoWiredanNotationBeanPostProcessor: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配置中也有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> {

共有1个答案

巢嘉志
2023-03-14

这是由于您将bean定义为stockservice而将其引用为stockservice造成的,这应该是相同的名称,在服务和控制器中区分大小写。

因此bean定义应该更新自:

@Service("StockService")

致:

@Service("stockService")
@Autowired
private StockService stockService;

还要确保您的bean是在Spring的扫描包中定义的。

 类似资料: