I'm new to Spring and Spring Data and I have created simple app. However, my app's controller keep failing to autowire the repository even though I have one
CustomerController.java
package controllers;
import persistence.domains.Customer;
import repositories.CustomerService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/customers")
@Service
public class CustomerController {
private CustomerService customerRepository;
@Autowired
public CustomerController(CustomerService customerRepository) {
this.customerRepository = customerRepository;
}
@RequestMapping(method = RequestMethod.GET)
public List<Customer> getAllCustomers() {
return customerRepository.findAll();
}
}
CustomerRepository.java
package persistence.repositories;
import persistence.domains.Customer;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepository extends CrudRepository<Customer,Long> {
public Customer save(Customer customer);
public Customer findOneByName(String name);
public List<Customer> findAll();
}
CustomerService.java
package persistence.repositories;
import persistence.domains.Customer;
import java.util.List;
public interface CustomerService {
/**
* Return all customers
* @return A list of all customers
*/
public List<Customer> findAll();
}
RepositoryCustomerService.java
package persistence.repositories;
import persistence.domains.Customer;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
public class RepositoryCustomerService implements CustomerService {
@Resource
private CustomerRepository repo;
@Transactional
public List<Customer> findAll() {
return repo.findAll();
}
}
Aucun commentaire:
Enregistrer un commentaire