简体   繁体   中英

Spring bean not identified with @Repository annotation

I have written a sample spring boot application and it is failing to run with message

` Description: Field customerRepository in com.hibernatetutorial.service.CustomerServiceImpl required a bean of type 'com.hibernatetutorial.repository.CustomerRepository' that could not be found.

Action: Consider defining a bean of type 'com.hibernatetutorial.repository.CustomerRepository' in your configuration.`

I have a @Repository annotation on CustomerRepository class and it's package is the there in base package scanning.

Below is configuration

   @SpringBootApplication
@ComponentScan(basePackages="com.hibernatetutorial")
public class HibernateTutorialApplication {

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

    }

}

@Repository
@Transactional
public interface CustomerRepository extends JpaRepository<Customer, UUID>{

}

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    public Customer createCustomer(Customer customer) {
        return customerRepository.save(customer);
    }

}

Customer entity is annotated with @Entity. Any suggestion if I miss anything

To make use of JpaRepository you need to add one of the following to your Application:

@EnableAutoConfiguration for Spring Boot to figure it out itself or

@EnableJpaRespositories(basePackageScan="com.example") to specify it yourself

For more information

请确认您的CustomerRepository和CustomerServiceImpl Java文件在com.hibernatetutorial相同的软件包中。

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