当我尝试自动连接扩展CrudRepository的接口时出现此错误。我有两个数据库的两个休眠xml配置。整个堆栈是
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'helloController'的bean时出错:通过字段'stockService'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'com.publishing.test.stock.services.StockService'的合格Bean:预计至少有1个有资格作为自动装配候选的Bean。依赖项注释:{@ 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> {
答案 0 :(得分:2)
这是由于以下事实造成的:您将bean定义为StockService
,并且将其引用为stockService
,这应该是相同的名称,在服务和控制器中都区分大小写。
因此,应从以下位置更新bean定义:
@Service("StockService")
收件人:
@Service("stockService")
因为要使用stockService
来引用它,所以它在以下位置的控制器中:
@Autowired
private StockService stockService;
注意:
还要确保您的bean是在spring的扫描软件包中定义的。