java - 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", ... })
解决方案
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 yourJpaRepository
s are declared. - or you specify multiple
String
values that correspond to each package of each of yourJpaRepository
s.
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.
推荐阅读
- javascript - 如何存储谷歌浏览器扩展的按钮保存状态
- algorithm - 给定“截止日期”,二分搜索是否最优?
- maven - Maven 模块和 quarkus 编译/实时重新加载
- rust - 数据互斥带来的 Rust 生命周期问题
- python - OpenCV:比较来自网络摄像头和图像的姿势
- c++ - 如何使用 std::cin 将空格作为输入插入到 char 数组中
- java - 如何在 Java 中的对象数组中通过方法访问实例?
- python - 我正在尝试按截止日期添加搜索。我怎样才能在 django 中做到这一点?
- python - Discord bot - 如果单词不存在则忽略命令
- c - st_size 为负但没有错误