2

I'm trying to configure a datasource with Spring Boot but I'm getting org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mycompany.myapp.repository.UserRepository com.mycompany.myapp.service.AuthenticationServiceImpl.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mycompany.myapp.repository.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

These is my project structure:

Main Class:

package com.mycompany.myapp;
@ComponentScan(basePackageClasses = {com.mycompany.myapp.domain.user.User.class,
                                     com.mycompany.myapp.repository.UserRepository.class,
                                     com.mycompany.myapp.service.AuthenticationServiceImpl.class})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Domain Class:

package com.mycompany.myapp.domain.user
@Entity
public class User {

    @Id
    @GeneratedValue
    private long id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private String lastName;
    @Column(nullable = false)
    private String password;
    @Column(nullable = false)
    private String email;

    public User() {}

    public User(String email, String password){
        this.email = email;
        this.password = password;
    }
}

Repository:

package com.mycompany.myapp.repository;
public interface UserRepository extends CrudRepository<User, Long> {

    List<User> findByLastName(String lastName);
}

Controller

package com.mycompany.myapp.service;
@RestController
public class AuthenticationServiceImpl implements AuthenticationService {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping("/add")
    public User add(){
        User user = new User();
        user.setName("Juan");
        user.setLastName("Sarpe");
        user.setEmail("email@gmail.com");
        userRepository.save(user);
        return user;
    }
}

application.properties

spring.datasource.url:jdbc:mysql://localhost:3306/mydb
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update

I guess my app is not detecting my @Repository annotation on UserRepository. As far as I know, Spring Boot will automatically set my class as @Repository because it is extending CrudRepository. What am I doing wrong? (Notice that my Application class is on top of packages hierarchy).

Marcos
  • 701
  • 1
  • 8
  • 25
  • Looks OK to me. Is that all of the exception? Any nested exceptions? Check for another class with the same name(UserRepository). Do you have the right maven/gradle dependencies? Take a look at this guide: https://spring.io/guides/gs/accessing-data-jpa/ – Evgeni Dimitrov Jun 03 '16 at 17:57
  • `ar.com.prokarma.tim.repository.UserRepository` is this a separate Class? – Jos Jun 03 '16 at 17:59
  • Sorry, i have copied the code and exception and tried to run it. I have edited the description. I will add the exception before the stacktrace to the description. – Marcos Jun 03 '16 at 18:17
  • You're missing @SpringBootApplication on the Application class. Please refer to the [Spring documentation](http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html) because package structure is very important. – kab Jun 03 '16 at 20:54
  • Sorry, I´ve already tried setting `@SpringBootApplication` on my main class. But it won't identify my `@Repository`. That's why I tried setting it that way.. – Marcos Jun 04 '16 at 18:17
  • Did you latter get to solve this and which of these solutions work or how did you you get around this – olyjosh Oct 21 '17 at 10:42

3 Answers3

2

In main class of spring boot you have to use this below annotation :

@SpringBootApplication
@ComponentScan(basePackages = "basepackage")
@EntityScan(basePackages ="basepackage")
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "basepackage")

In Repository layer use below annotation :

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

@Transactional
@Repositor

If you have any service layer then use below annotation in the implementation class:

import org.springframework.stereotype.Service;

@Service
Sumeet Tiwari
  • 343
  • 2
  • 11
1

Have you tried @EnableJpaRepositories in your configuration ?

Akli REGUIG
  • 552
  • 4
  • 13
0

The standard way to configure is to create a base marker repository interface and provide that in the component scan.

public interface RepositoryPackage {
}

Keep the interface RepositoryPackage in the same package as UserRepository.

@ComponentScan(basePackageClasses= "RepositoryPackage.class")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
notionquest
  • 37,595
  • 6
  • 111
  • 105