0

Note: I am not using Spring Boot

I am getting the error as below

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.repository.MbrEnrollRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

Here is My Repository, which is extending crud repository of spring data jdbc

import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
public interface MbrEnrollRepository extends CrudRepository<EM_MBR_ENROLLMENT_VO, String> { 
@Query("select count(*) FROM EM_MBR_ENROLLMENT WHERE MEMBER_ID ='W0000000002'")
  int countItems();
}

I am calling the countItems() method in my service class.According to my knowledge spring container will create an instance of repository, for that we need to define the bean of type repository in configuration class. I have not found anywhere that, how to define a bean for repository.

public class MbrServicesImpl implements MbrServices {
      @Autowired
      private MbrEnrollRepository  repository;  
      public int getEnrollCount() {
         return (int) repository.count();
       }
}

I have defiend requried beans in my configuration class as shown in spring docs.

@EnableWebMvc
@ComponentScan(basePackages = "com.restControllers")
@Configuration
@EnableJdbcRepositories
public class MyBeansConfiguration extends JdbcConfiguration{

   @Bean
   public DriverManagerDataSource dataSource() {
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("MYDRIVER");
        ds.setUrl("MYURL");
        ds.setUsername("USER");
        ds.setPassword("PASSWORD");
        return ds;
    }
    @Bean
    NamedParameterJdbcOperations operations() {
        return new NamedParameterJdbcTemplate(dataSource());
    }

    @Bean
    PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public MbrServices mbrServicesImpl() {
          MbrServicesImpl mbrServices = new MbrServicesImpl();
          return mbrServices;
    }
}

My Controller class

@RestController
public class MyRestController{
     @Autowired
      private MbrServices mbrServicesImpl;
      @RequestMapping("/count")
      public String getCount() {
             System.out.println("Inside rest controller method : COUNT ");
             return String.valueOf(service.getEnrollCount());
      }
}

Is it possible to use Spring data JDBC with spring 4+ ? if yes, Please help me....

paardhu
  • 101
  • 8

1 Answers1

0

Spring Data JDBC requires Spring 5.x

While it is perfectly fine to use Spring Data JDBC without Spring Boot it won't work with Spring 4.

There might already be a MethodNotFound or similar exception somewhere in the stack trace you have. And if it isn't it is almost bound to appear once other problems are fixed.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348