0

I wanted to move from JdbcTemplate to Spring Data JDBC. However I seem to have some misconfiguration but I cannot figure out where. The errors are "expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}" and "Parameter 0 of constructor ... required a bean ... that could not be found."

I put @Repository on the public repository interfaces extending PagingAndSortingRepository (as I did with the DAO classes extending from JdbcDaoSupport) without success. Then I added @EnableJdbcRepositories with and without package name to the database config class, also no success. I also tried the database config to inherit from AbstractJdbcConfiguration, still the same errors ...

Unfortunately I couldn't find a working example and I now gave up after some trial and error. I still would love to get this working, the version I used is spring-boot-starter-data-jdbc:2.4.0

Code fragments:

DatabaseConfiguration.java

@Configuration
@EnableJdbcRepositories("<basepackage>.repository.jdbc")
@EnableJdbcAuditing(auditorAwareRef = "springSecurityAuditorAware")
@EnableJpaRepositories("<basepackage>.repository")
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
@EnableTransactionManagement
@EnableElasticsearchRepositories("<basepackage>.repository.search")
public class DatabaseConfiguration extends AbstractJdbcConfiguration {

}

UserRepository.java

@Repository
public interface UserRepository extends PagingAndSortingRepository<User, String> {

}

QualityResource.java (REST Controller)

public QualityResource(UserRepository userRepository) {
    this.userRepository = userRepository;
}

Error messages:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'qualityResource' defined in file [.../backend/build/classes/java/main/.../web/rest/QualityResource.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '....repository.jdbc.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Application failed to start: Description: Parameter 0 of constructor in <basepackage>.web.rest.QualityResource required a bean of type '<basepackage>.repository.jdbc.UserRepository' that could not be found.
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
werwuifi
  • 367
  • 1
  • 4
  • 15
  • Please post actual code and full stack traces. – Jens Schauder Dec 10 '20 at 14:33
  • done, please check again – werwuifi Dec 13 '20 at 19:19
  • Do you have a `@Table` annotation on your `User` entity? – Jens Schauder Dec 14 '20 at 06:21
  • I am using import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.Column; public class User { @Id @Column("username") private String username; ... } only but I will give it a try – werwuifi Dec 14 '20 at 06:43
  • Unfortunately adding the Table annotation doesn't change anything ... – werwuifi Dec 14 '20 at 06:53
  • Side note: you don't need the `@Repository` annotation, it is already included in the inheritance tree. – Jens Schauder Dec 14 '20 at 09:03
  • tried this as well, still no luck :( – werwuifi Dec 14 '20 at 09:42
  • Yeah, that doesn't fix anything, it is just superfluous. I can't tell what is wrong. If you had a github repository I could look into though ... – Jens Schauder Dec 14 '20 at 09:52
  • It's a project for a customer I can check if you can get access. Do you have an email address I could send the link to? Thanks a lot for your efforts!! – werwuifi Dec 14 '20 at 10:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225926/discussion-between-jens-schauder-and-werwuifi). – Jens Schauder Dec 14 '20 at 10:14
  • I posted the details in the chat. Thx! – werwuifi Dec 15 '20 at 08:34
  • It looks like the chat doesn't produce notifications so it's rather useless. I would need a small actually executable project, preferable as a github repository. – Jens Schauder Dec 15 '20 at 10:09
  • Ok for some reason I get a different error now: "Invocation of init method failed; nested exception is java.lang.IllegalStateException: No query specified on findAll." After adding the method to the interface it still remains. This is what I added: @org.springframework.data.jdbc.repository.query.Query("...") @Override Page findAll(Pageable pageable); – werwuifi Dec 15 '20 at 20:08

0 Answers0