首页 > 解决方案 > Spring - 将存储库自动装配到服务中时出现 NoSuchBeanDefinitionException

问题描述

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

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", ... })

标签: javaspringservicedependency-injectionrepository

解决方案


This annotation :

@EnableJpaRepositories(basePackages = ...)

will enable JPA repositories according to the value given to the basePackages attribute.
Generally you have two ways of using it :

  • either you specify a single String value that corresponds to the package or the parent package where your JpaRepositorys are declared.
  • or you specify multiple String values that correspond to each package of each of your JpaRepositorys.

Actually, you seem using the second strategy as it works for some Repositories but not for this one. So you have to explicitly add the package of this repository :

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

Note that the first way (using a single value as basePackages ) should be favored as much as possible as it allows to add/remove of new Repository without the need to update the basePackages annotation.


推荐阅读