0
votes

We are getting No qualifying bean of type for below code in spring boot:

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);     

    }
}

Added below dependencies related to MySQL: pom.xml:

<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>

getting error while testing this:

@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
    }
}

Error details as below:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DBUtils': Unsatisfied dependency expressed through field 'bnRepo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '..*.dataaccess.repository.BankRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

1

1 Answers

0
votes

BankRepository is not created hence the error - there is no bean of that type so there is nothing to inject (via autowiring).

You should check the following:

  1. Spring Data is enabled
  2. The BankRepository should reside in the package that is subject to package scanning in your spring boot application