๐Ÿ“˜ Programming/Spring

[Spring Framework] 3. XML ๊ธฐ๋ฐ˜์˜ ์˜์กด์„ฑ ์ฃผ์ž…(DI) ๋ฐฉ๋ฒ• ๋ฐ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ (3)

ํ•œ์ฝ”๋”ฉ 2021. 8. 7. 22:39
728x90
728x90

ํ•™์Šต ๋ชฉํ‘œ

1. '์˜์กด์„ฑ ์ฃผ์ž…' ๊ธฐ๋ณธ ๊ฐœ๋… ์ดํ•ด
2. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์™€ ApplicationContext ์ดํ•ด
3-1. XML ํŒŒ์ผ์„ ์ด์šฉํ•œ ์˜์กด์„ฑ ์ฃผ์ž…(DI) ์„ค์ • ๋ฐฉ๋ฒ•
3-2. Annotation์„ ์ด์šฉํ•œ ์˜์กด์„ฑ ์ฃผ์ž…(DI) ์„ค์ • ๋ฐฉ๋ฒ•
3-3. Java๋ฅผ ์ด์šฉํ•œ ์˜์กด์„ฑ ์ฃผ์ž…(DI) ์„ค์ • ๋ฐฉ๋ฒ•
4. Bean ๊ฐ์ฒด์˜ Scope์™€ LifeCycle ์ดํ•ด

XML ๊ธฐ๋ฐ˜ DI ๊ตฌ์„ฑ

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋ฐ ํ™˜๊ฒฝ

ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ๋„

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ ์ˆ˜์ •

Java ๋ฒ„์ „ ๋ณ€๊ฒฝ

์˜์กด์„ฑ ์ถ”๊ฐ€ ๋ฐ ๋ณ€๊ฒฝ (pom.xml)

  • spring-context → ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ๋ฒ„์ „ ์ ์šฉ (mvnrepositroy.com ์ฐธ๊ณ )
  • ๋กœ๊น… ํ”„๋ ˆ์ž„์›Œํฌ ์ ์šฉ → slf4j-api, logback-classic ์ ์šฉ

applicationContext.xml ์ƒ์„ฑ

  • Spring Bean Configuration File๋กœ applicationContext.xml ํŒŒ์ผ ์ƒ์„ฑ
  • Namespace ์ถ”๊ฐ€ → beans, c, context ์ถ”๊ฐ€
    • beans : Bean ์ปดํฌ๋„ŒํŠธ ์„ค์ •
    • c : ์ƒ์„ฑ์ž ์ธ์ž๋ฅผ <bean> ์š”์†Œ ์• ํŠธ๋ฆฌ๋ทฐํŠธ๋กœ ์„ ์–ธ
    • context : Bean ๊ฒ€์ƒ‰๊ณผ ์–ด๋…ธํ…Œ์ด์…˜ ์„ค์ •

 

XML ๊ธฐ๋ฐ˜ DI ํ”„๋กœ์ ํŠธ ์†Œ์Šค์ฝ”๋“œ

StudentVO.java

package org.kpu.di.domain;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class StudentVO {
	private String id;
	private String passwd;
	private String username;
	private String snum;
	private String depart;
	private String mobile;
	private String email;
}

 

MemberSampleMain.java

package org.kpu.di.main;

import org.kpu.di.domain.StudentVO;
import org.kpu.di.service.MemberService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class MemberSampleMain {

	private static ApplicationContext ctx = null;

	public static void main(String[] args) {

		System.out.println("์•ˆ๋…•ํ•˜์„ธ์š” DI-XML");

		// ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
		ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");

		// ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ปดํฌ๋„ˆ๋Š” ๊ฐ€์ ธ์˜ด
		MemberService memberService = (MemberService) ctx.getBean("memberSerivce");

		StudentVO vo = new StudentVO();
		vo.setId("kandara");

		StudentVO member = new StudentVO();
		System.out.println(member);
	}
}

 

MemberDAO.java

package org.kpu.di.persistence;

import org.kpu.di.domain.StudentVO;

public interface MemberDAO {
	public StudentVO read(String id) throws Exception;
	public void add(StudentVO student) throws Exception;
}

 

MemberDAOImpl.java

package org.kpu.di.persistence;

import java.util.HashMap;
import java.util.Map;

import org.kpu.di.domain.StudentVO;

public class MemberDAOImpl implements MemberDAO {

	private Map<String, StudentVO> storage = new HashMap<String, StudentVO>();

	public StudentVO read(String id) throws Exception {
		return storage.get(id);
	}

	public void add(StudentVO student) throws Exception {
		storage.put(student.getId(), student);
	}
}

 

MemberService.java

package org.kpu.di.service;

import org.kpu.di.domain.StudentVO;

public interface MemberService {
	public StudentVO readMember(String userid) throws Exception;
	public void addMember(StudentVO student)throws Exception;
}

 

MemberServiceImpl.java

package org.kpu.di.service;

import org.kpu.di.domain.StudentVO;
import org.kpu.di.persistence.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);
	}

}

 

 

์˜์กด์„ฑ ์ฃผ์ž… ๋ฐฉ์‹

์ƒ์„ฑ์ž ๊ธฐ๋ฐ˜ ์˜์กด์„ฑ ์ฃผ์ž…

  • ์ƒ์„ฑ์ž์˜ ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉ
  • ์„ค์ •ํŒŒ์ผ XML์— <costructor-arg> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด ์ฃผ์ž…ํ•  ์ปดํฌ๋„ŒํŠธ ์„ค์ •

 

์„ค์ •์ž ๊ธฐ๋ฐ˜ ์˜์กด์„ฑ ์ฃผ์ž…

  • ๋ฉ”์†Œ๋“œ์˜ ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉ
  • ์„ค์ •ํŒŒ์ผ XML์— <property> ์š”์†Œ์˜ name ์†์„ฑ์— ์ฃผ์ž…ํ•  ์ปดํฌ๋„ŒํŠธ์˜ ์ด๋ฆ„์„ ์„ค์ •

bean ํƒœ๊ทธ์˜ ์†์„ฑ

  1. id : ์˜ค๋ธŒ์ ํŠธ ์‹๋ณ„ id
  2. name : ์˜ค๋ธŒ์ ํŠธ ๋ช…
  3. class : id์˜ ์‹ค์ฒด๋กœ ํŒจํ‚ค์ง€๋ช…๊ณผ ํด๋ž˜์Šค๋ช…์œผ๋กœ ๊ตฌ์„ฑ
  4. scope : ์˜ค๋ธŒ์ ํŠธ ์Šค์ฝ”ํ”„ ์ง€์ •
  5. parent : ์„ค์ •์„ ๋ฌผ๋ ค๋ฐ›์„ ์˜ค๋ธŒ์ ํŠธ๋ช… ์ง€์ •
  6. abstract : ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์œ ๋ฌด (๊ธฐ๋ณธ๊ฐ’ : false)
  7. singleton : getBean ๋ฉ”์†Œ๋“œ๋กœ ์–ป๋Š” ์ธ์Šคํ„ด์Šค์„ ์ธ์Šคํ„ด์Šคํ™” (๊ธฐ๋ณธ๊ฐ’ : true)
  8. lazy-init : ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์ง€์—ฐ (๊ธฐ๋ณธ๊ฐ’ : false)
  9. depend-on : ์˜์กด ๊ด€๊ณ„์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
  10. init-method : ๋ฉ”์†Œ๋“œ ๋ช…์„ ๊ธฐ์ˆ ํ•ด ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์˜ ์„ค์ • ํ›„์— ํ˜ธ์ถœ
  11. destroy-method : ๋ฉ”์†Œ๋“œ ๋ช…์„ ๊ธฐ์ˆ ํ•ด ์‹œ์Šคํ…œ ์ข…๋ฃŒ ์‹œ ํ˜ธ์ถœ
  12. parent : ์„ค์ •์„ ๋ฌผ๋ ค๋ฐ›์„ ์˜ค๋ธŒ์ ํŠธ ๋ช… ์ง€์ •
  13. autowire
    • no
    • byName
    • byType
    • constructor
728x90
๋ฐ˜์‘ํ˜•