Spring - 将存储库自动装配到服务中时的NoSuchBeanDefinitionException

时间:2018-05-22 15:45:21

标签: java spring service dependency-injection repository

我正在尝试将存储库自动装入从控制器调用的服务,但我不断收到以下错误消息:

Error creating bean with name 'mktPlace2PedidoService' defined in VFS resource [\"/home/davidgarcia/wildfly-11.0.0.Final/standalone/deployments/issuer.war/WEB-INF/classes/br/com/sinergico/service/mktplace2/MktPlace2PedidoService.class\"]: Unsatisfied dependency expressed through constructor argument with index 0 of type [br.com.sinergico.repository.mktplace2.MktPlace2PedidoRepository]: No qualifying bean of type [br.com.sinergico.repository.mktplace2.MktPlace2PedidoRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [br.com.sinergico.repository.mktplace2.MktPlace2PedidoRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

奇怪的是,我有许多其他存储库以完全相同的方式使用,但没有给出这个问题......

以下是我对服务构造函数

中存储库的调用
package br.com.sinergico.service.mktplace2;

@Service
public class MktPlace2PedidoService extends GenericService<MktPlace2Pedido, Long> {

    private MktPlace2PedidoRepository repository;

    @Autowired
    public MktPlace2PedidoService(MktPlace2PedidoRepository repository) {
        super(repository);
        this.repository = repository;
    }

    //other methods
}

这是存储库

package br.com.sinergico.repository.mktplace2;

public interface MktPlace2PedidoRepository extends JpaRepository<MktPlace2Pedido, Long> {
    //some methods   
}

这是实体声明

package br.com.entity.mktplace2;

@Entity
@Table(name="pedido", schema = "marketplace")
@SequenceGenerator(name="hibernate_sequence",    sequenceName="marketplace.hibernate_sequence",initialValue=1, allocationSize=1)
public class MktPlace2Pedido implements Serializable{

    private static final long serialVersionUID = 45381343130121532L;

    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hibernate_sequence")
    private Long id;

    //other columns

}

我尝试将@Repository注释包含在存储库中,但它没有做任何事情。

有人可以帮忙吗?我花了好几个小时才弄明白这一点......

编辑 - 解决方案

感谢davidxxx,问题解决了。我错过了@EnableJpaRepositories中的包,现在它看起来像这样:

@EnableJpaRepositories(basePackages = { ... ,"br.com.sinergico.repository.mktplace2", ... })

3 个答案:

答案 0 :(得分:1)

您没有创建MktPlace2PedidoRepository的任何bean。使用@Repository注释您的界面。 另一个想法,使用@EnableJpaRepositories(&#34; br.com.sinergico.repository&#34;)给Spring信息在哪里找到创建bean的Repository类

答案 1 :(得分:1)

此注释:

@EnableJpaRepositories(basePackages = ...)

将根据basePackages属性的值启用JPA存储库 通常,您有两种使用方式:

  • 要么指定与包对应的单个String值,要么指定声明JpaRepository的父包。
  • 或指定多个String值,这些值对应于每个JpaRepository的每个包。

实际上,您似乎使用第二种策略,因为它适用于某些存储库但不适用于此存储库。所以你必须显式添加这个存储库的包:

@EnableJpaRepositories(basePackages = { ... ,"br.com.sinergico.repository.mktplace2", ... })

请注意,第一种方式(使用单个值为basePackages)应该尽可能地受到青睐,因为它允许添加/删除新的存储库而无需更新basePackages注释。

答案 2 :(得分:0)

为Repository接口MktPlace2PedidoRepository添加@Repository Annotation

此外,

从MktPlace2PedidoService中删除构造函数并为存储库字段添加@Autowired