gpt4 book ai didi

java - 没有可用类型的合格 bean(Spring Data)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:25 25 4
gpt4 key购买 nike

当我尝试 Autowiring 扩展 CrudRepository 的接口(interface)时出现此错误。我有两个用于 2 个数据库的 hibernate xml 配置。完整的堆栈是

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;

我在 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> {

enter image description here

最佳答案

这是因为您将 bean 定义为 StockService 而您将其称为 stockService,这应该是相同的名称,两者都区分大小写服务和 Controller 。

因此 bean 定义应该更新自:

@Service("StockService")

收件人:

@Service("stockService")

因为您在 Controller 中用 stockService 引用它:

@Autowired
private StockService stockService;

注意:

还要确保你的bean在扫描的spring包中定义。

关于java - 没有可用类型的合格 bean(Spring Data),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51746284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com