[英]UnsatisfiedDependencyException in Spring Boot
We are getting No qualifying bean of type for below code in spring boot:我们在 spring 引导中得到以下代码的类型没有合格 bean:
Main class:
package *.*.*;
@SpringBootApplication
public class BankApplication {
public static void main(String[] args) {
SpringApplication.run(BankApplication.class, args);
}
}
package *.*.*.dataaccess.repository;
@Repository
public interface BankRepository extends JpaRepository<BankEntity, String> {
}
package *.*.*.dataaccess.dao;
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Bank")
public class BankEntity {
//fileds
}
package *.*.*.dataaccess.utils;
@Component
public class DBUtils {
@Autowired
BankRepository bnRepo;
*/
public void saveBank(BankDto dto) {
//preparing bnEntity -entity
this.bnRepo.saveAndFlush(bnEntity);
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.wix/wix-embedded-mysql -->
<dependency>
<groupId>com.wix</groupId>
<artifactId>wix-embedded-mysql</artifactId>
<version>4.2.0</version>
</dependency>
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { DBUtils.class })
public class BankTest {
@Autowired
DBUtils dbUtils;
@Test
public void saveAndFlishTest() {
dbUtils.saveBank(getBankDetails()); // once this is working then will added junit checks using asserts
}
}
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DBUtils': Unsatisfied dependency expressed through field 'bnRepo';
原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“DBUtils”的bean时出错:通过字段“bnRepo”表示的依赖关系不满足; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ' .
嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ' 。 .*.dataaccess.repository.BankRepository' available: expected at least 1 bean which qualifies as autowire candidate.
.*.dataaccess.repository.BankRepository' 可用:预计至少有 1 个符合自动装配候选资格的 bean。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
BankRepository
is not created hence the error - there is no bean of that type so there is nothing to inject (via autowiring). BankRepository
未创建,因此出现错误 - 没有该类型的 bean,因此没有任何东西可以注入(通过自动装配)。
You should check the following:您应该检查以下内容:
BankRepository
should reside in the package that is subject to package scanning in your spring boot application BankRepository
应驻留在 package 扫描的 package 中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.