// Can have three way to do the wiring, field, constructor and setter
package com.pluralsight.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pluralsight.model.Customer;
import com.pluralsight.repository.CustomerRepository;
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
// @Autowired
private CustomerRepository customerRepository;
@Autowired
public CustomerServiceImpl(CustomerRepository customerRepository){
this.customerRepository = customerRepository;
}
@Override
public List<Customer> findAll(){
return customerRepository.findAll();
}
// @Autowired
public void setCustomerRepository(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.pluralsight.service.CustomerService;
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
// CustomerService service = new CustomerServiceImpl();
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService service = appContext.getBean("customerService", CustomerService.class);
System.out.println(service.findAll().get(0).getFirstName());
}
}