![](/img/trans.png)
[英]@Autowired cant find repository bean identified with @Repository
[英]Spring bean not identified with @Repository annotation
我已经编写了一个示例spring boot应用程序,它无法通过消息运行
`描述:com.hibernatetutorial.service.CustomerServiceImpl中的字段customerRepository需要一个类型为'com.hibernatetutorial.repository.CustomerRepository'的bean。
行动:考虑在您的配置中定义一个类型为“ com.hibernatetutorial.repository.CustomerRepository”的bean。
我在CustomerRepository类上有一个@Repository批注,它的包位于基本包扫描中。
下面是配置
@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);
}
}
客户实体用@Entity注释。 如果我错过任何建议
要使用JpaRepository,您需要向应用程序中添加以下内容之一:
@EnableAutoConfiguration
for Spring Boot可以自行解决或
@EnableJpaRespositories(basePackageScan="com.example")
指定
请确认您的CustomerRepository和CustomerServiceImpl Java文件在com.hibernatetutorial相同的软件包中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.