简体   繁体   中英

Spring boot CrudRepo Define a bean

I have a problem in spring boot concerning repositories. I have a service :

@Service("userService")
public class UserServiceImpl implements UserService {

  @Autowired
  private UserRepository userRepository;

  @Autowired
  private RoleRepository roleRepository;
}

and the repos here :

@Repository("userRepository")
public interface UserRepository extends CrudRepository<User, Long> {
     User findByEmail(String email);
}

When I run the app I get this message :

Description:

Field userRepository in com.projectWS.service.UserServiceImpl required a 
bean of type 'com.projectWS.repo.UserRepository' that could not be found.

Action:

Consider defining a bean of type 'com.projectWS.repo.UserRepository' in your 
configuration.

Please help me I'm so desperate... this is my main class:

@SpringBootApplication
@Configuration
@EnableWebMvc
public class Main {

   public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
   }
}

Another problem is , @EnableJpaRepositories doesn't get recognized !!

most likely your Main class is defined within a package and your other packages are not getting scanned.

Try annotation your Main class with:

@ComponentScan("com.projectWS")

judging by your error message and assuming that the top of your package level starts at com.projectWS

I am not a Spring expert but I suspect that this can be because of the case of the name of these classes. Just for peace of mind test this please:

@Service("userService")
public class UserServiceImpl implements UserService {

  @Qualifier("userRepository")
  @Autowired
  private UserRepository userRepository;

  @Qualifier("roleRepository")
  @Autowired
  private RoleRepository roleRepository;
}

It seems like you have not added Spring data for JPA, add the following to your pom.xml

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM