프로젝트를 셋팅한 뒤 pakage를 만든다
customer.domain : Customer.java
customer.main : CustomerRun.java
customer.Service :CustomerServide 인터페이스
ServiceLogic
customer.store : CustomerStore 인터페이스
StoreLogic
package org.kh.customer.domain;
public class Customer {
private String id;
private String name;
private String address;
private String email;
public Customer() {}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", email=" + email + "]";
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class CustomerRun {
public static void main(String[] args) {
ApplicationContext ctx = new GenericXmlApplicationContext("");
}
}
public interface CustomerService {
public List<Customer> findA11();
}
public class CustomerServiceImple implements CustomerService{
//private CustomerStoreLogic cStore //현재 강한결합상태 해소하기 위해 인터페이스로 바꾼다
private CustomerStore cStore;
public void setStore(CustomerStore cStore) {
this.cStore = cStore;
}//이게 DI를 만들기 위해 만든 setter
//인터페이스로 선언하여 강한결합을 해소할수있다.
//강한결합을 해소하지 않으면 CustomerStoreLogic이 변화될때마다 고쳐줘야함
//유지보수의 편리성
//결합을 해소하는 방법 DI 의존성 주입
//public CustomerServiceImple() {
//cStore = new CustomerStoreLogic(); //DI로 이 결합을 해소할것임
//}
@Override
public List<Customer> findA11() {
List<Customer> cList = new CustomerStoreLogic().selectAll();
return cList;
}
}
public interface CustomerStore {
public List<Customer> selectAll();
}
public class CustomerStoreLogic implements CustomerStore{
@Override
public List<Customer> selectAll() {
List<Customer> cList = new ArrayList<Customer>();
for(int i =0;i<10;i++) {
Customer cOne = new Customer();
cOne.setId(i+"");
cOne.setName(i+"");
cOne.setAddress(String.valueOf(i));
cOne.setEmail(new StringBuffer(0).append(i).toString());
cList.add(cOne);
}
return cList;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="customerService" class="org.kh.customer.service.logic.CustomerServiceImple">
<property name="store" ref="customerStore"></property>
</bean>
<bean id ="customerStore" class="org.kh.customer.store.logic.CustomerStoreLogic">
</bean>
</beans>
public class CustomerRun {
public static void main(String[] args) {
ApplicationContext ctx = new GenericXmlApplicationContext("spring-context.xml");
CustomerService cService = (CustomerServiceImple)ctx.getBean("customerService");
List<Customer> cList= cService.findA11();
for(Customer cOne : cList) {
System.out.println(cOne.toString());
}
}
}
이해하려면 시간이 필요할 듯 하다