src/main/resources/applicationContext.xml 생성
src/main/resources/applicationContext.xml 네임스페이스 추가
beans 네임 스페이스 : 빈(컴포넌트) 설정
C 네임 스페이스 : 생성자 인자를 <bean
> 요소 어트리뷰트로 선언
context 네임 스페이스 : 빈 검색과 어노테이션 설정 (ex. context:component-scan)
StudentVO.java
package org.tukorea.di.domain;
public class StudentVO {
private String id;
private String passwd;
private String username;
private String snum;
private String depart;
private String mobile;
private String email;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSnum() {
return snum;
}
public void setSnum(String snum) {
this.snum = snum;
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "StudentVO [id=" + id + ", passwd=" + passwd + ", username=" + username + ", snum=" + snum + ", depart="
+ depart + ", mobile=" + mobile + ", email=" + email + "]";
}
}
MemberrDAO.java
package org.tukorea.di.persistance;
import org.tukorea.di.domain.StudentVO;
public interface MemberDAO {
public StudentVO read (String id) throws Exception;
public void add(StudentVO student) throws Exception;
}
MemberDAOImpl.java
package org.tukorea.di.persistance;
import java.util.HashMap;
import java.util.Map;
import org.tukorea.di.domain.StudentVO;
public class MemberDAOImpl implements MemberDAO {
public Map<String, StudentVO> storage = new HashMap<String, StudentVO>();
@Override
public StudentVO read(String id) throws Exception {
// TODO Auto-generated method stub
return storage.get(id);
}
@Override
public void add(StudentVO student) throws Exception {
// TODO Auto-generated method stub
storage.put(student.getId(), student);
}
}
MemberService.java
package org.tukorea.di.service;
import org.tukorea.di.domain.StudentVO;
public interface MemberService {
public StudentVO readMember(String userid) throws Exception;
public void addMember(StudentVO student) throws Exception;
}
MemberServiceImpl.java
package org.tukorea.di.service;
import org.tukorea.di.domain.StudentVO;
import org.tukorea.di.persistance.MemberDAO;
public class MemberServiceImpl implements MemberService {
private MemberDAO memberDAO;
public MemberServiceImpl(MemberDAO memberDAO) {
this.memberDAO = memberDAO;
}
public StudentVO readMember(String userid) throws Exception {
return memberDAO.read(userid);
}
public void addMember(StudentVO student) throws Exception {
memberDAO.add(student);
}
}
MemberSampleMain.java
package org.tukorea.di.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.tukorea.di.domain.StudentVO;
import org.tukorea.di.service.MemberService;
public class MemberSampleMain {
private static ApplicationContext ctx = null;
public static void main(String[] args) throws Exception {
System.out.println("안녕하세요 DI-XML");
ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml"); // 스프링 컨테이너 생성
MemberService memberService = (MemberService)ctx.getBean("memberService"); // 스프링 컨테이너에서 컴포넌트를 가져온다
StudentVO vo = new StudentVO();
vo.setId("kanadara");
memberService.addMember(vo);
StudentVO member = memberService.readMember("kanadara");
System.out.println(member);
}
}
constructor-arg
>태그를 사용하여 주입할 컴포넌트 설정property
>요소의 name 속성에 주입할 컴포넌트의 이름을 설정