[SPRING] 🧩 Simple Project

μž„λ‚™ν˜„Β·2022λ…„ 10μ›” 20일
0

SpringFramework

λͺ©λ‘ 보기
7/7
post-thumbnail
post-custom-banner

μ €λ²ˆ ν¬μŠ€νŒ…μ—μ„œλŠ” DI에 κ΄€ν•˜μ—¬ 곡뢀해 λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

μ΄λ²ˆμ—λŠ” κ°„λ‹¨ν•œ ν”„λ‘œμ νŠΈλ₯Ό 톡해 Spring 곡뢀λ₯Ό ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.


0. πŸͺ΅ Make Project

μ•žμ„  ν¬μŠ€νŒ… 쀑 ν”„λ‘œμ νŠΈλ₯Ό import ν•˜λŠ” ν¬μŠ€νŒ…μ΄ μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

μ €λŠ” μ—¬κΈ°μ„œ importν•œ ν”„λ‘œμ νŠΈλ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜μ˜€μ§€λ§Œ μ € λ°©λ²•λŒ€λ‘œ μƒˆ Projectλ₯Ό import해도 λ¬΄λ°©ν•©λ‹ˆλ‹€.

Maven ν”„λ‘œμ νŠΈλ₯Ό μƒμ„±ν•˜κ³  Eclipse에 import ν•˜μ˜€κΈ° λ•Œλ¬Έμ— λ‹€μŒ 클래슀λ₯Ό μ°¨λ‘€λŒ€λ‘œ μž‘μ„±ν•˜κ² μŠ΅λ‹ˆλ‹€.

1. νšŒμ› 데이터 κ΄€λ ¨ 클래슀

  • Member
  • WrongIdPasswordException
  • MemberDao

2. νšŒμ› κ°€μž… 처리 κ΄€λ ¨ 클래슀

  • DuplicateMemberException
  • MemberRegisterService
  • RegisterRequest

3. μ•”ν˜Έ λ³€κ²½ κ΄€λ ¨ 클래슀

  • MemberNotFoundException
  • ChangePasswordService

1. πŸ›– νšŒμ› 데이터 κ΄€λ ¨ 클래슀

κ°€μž₯ λ¨Όμ € μž‘μ„±ν•  μ½”λ“œλŠ” Member클래슀 μž…λ‹ˆλ‹€. νšŒμ› 데이터λ₯Ό ν‘œν˜„ν•˜κΈ° μœ„ν•΄ 이 클래슀λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. μ½”λ“œλŠ” μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

01.CHAP01/src/main/java/spring/Member.java

package spring;

import java.time.LocalDateTime;

public class Member {

	private Long id;
	private String email;
	private String password;
	private String name;
	private LocalDateTime registerDateTime;
	
	public Member(String email, String password,
			String name, LocalDateTime regDateTime) {
		this.email = email;
		this.password = password;
		this.name = name;
		this.registerDateTime = regDateTime;
	}

	public Long getId() {
		return id;
	}

	void setId(Long id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public String getPassword() {
		return password;
	}

	public String getName() {
		return name;
	}

	public LocalDateTime getRegisterDateTime() {
		return registerDateTime;
	}
	
	public void changePassword(String oldPassword, String newPassword) {
		if(!password.equals(oldPassword))
			throw new WrongIdPasswordException();
		this.password = newPassword;
	}
	
}

λ§ˆμ§€λ§‰ λΆ€λΆ„μ˜ changePassword() λ©”μ„œλ“œλŠ” μ•”ν˜Έ λ³€κ²½ κΈ°λŠ₯을 κ΅¬ν˜„ν•œ κ²ƒμž…λ‹ˆλ‹€. 이 λ©”μ„œλ“œλŠ” oldPassword와 newPassword의 두 νŒŒλΌλ―Έν„°λ₯Ό μ „λ‹¬λ°›μŠ΅λ‹ˆλ‹€.

oldPasswordκ°€ ν˜„μž¬ μ•”ν˜ΈμΈ password ν•„λ“œμ™€ 값이 λ‹€λ₯΄λ©΄ WrongIdPasswordException을 λ°œμƒμ‹œν‚€κ³ , 값이 κ°™μœΌλ©΄ password ν•„λ“œλ₯Ό newPassword둜 λ³€κ²½ν•©λ‹ˆλ‹€.


이 λ©”μ„œλ“œμ—μ„œ μ‚¬μš©ν•œ WrongIdPasswordException ν΄λž˜μŠ€λŠ” μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

01.CHAP01/src/main/java/spring/WrongIdPasswordException.java

package spring;

public class WrongIdPasswordException extends RuntimeException {

}

λ‹€μŒμœΌλ‘œ μž‘μ„±ν•  μ½”λ“œλŠ” MemberDao ν΄λž˜μŠ€μž…λ‹ˆλ‹€. 아직 Spring을 μ΄μš©ν•΄μ„œ DBλ₯Ό μ—°λ™ν•˜μ§€ μ•Šμ•˜μœΌλ―€λ‘œ, 일단 Map을 μ΄μš©ν•΄μ„œ κ΅¬ν˜„ν•΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.

01.CHAP01/src/main/java/spring/MemberDao.java

package spring;

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

public class MemberDao {

	private static long nextId = 0;
	
	private Map<String, Member> map = new HashMap<>();
	
	public Member selectByEmail(String email) {
		return map.get(email);
	}
	
	public void insert(Member member) {
		member.setId(++nextId);
		map.put(member.getEmail(), member);
	}
	
	public void update(Member member) {
		map.put(member.getEmail(), member);
	}
}

2. πŸͺœ νšŒμ› κ°€μž… 처리 κ΄€λ ¨ 클래슀

νšŒμ› κ°€μž… μ²˜λ¦¬μ— ν•„μš”ν•œ ν΄λž˜μŠ€λŠ” DuplicateMemberException, RegisterRequest, MemberRegisterServiceμž…λ‹ˆλ‹€. μ½”λ“œλŠ” μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

01.CHAP01/src/main/java/spring/DuplicateMemberException.java

package spring;

public class DuplicateMemberException extends RuntimeException {

	public DuplicateMemberException(String message) {
		super(message);
	}
	
}

DuplicateMemberException ν΄λž˜μŠ€λŠ” λ™μΌν•œ 이메일을 κ°–κ³  μžˆλŠ” νšŒμ›μ΄ 이미 μ‘΄μž¬ν•  λ•Œ MemberRegisterServiceκ°€ λ°œμƒμ‹œν‚€λŠ” μ˜ˆμ™Έ νƒ€μž…μž…λ‹ˆλ‹€.


01.CHAP01/src/main/java/spring/MemberRegisterService.java

package spring;

import java.time.LocalDateTime;

public class MemberRegisterService {
	private MemberDao memberDao;
	
	public MemberRegisterService(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
	
	public Long regist(RegisterRequest req) {
		Member member = memberDao.selectByEmail(req.getEmail());
		if(member != null) {
			throw new DuplicateMemberException("dup email " + req.getEmail());
		}
		Member newMember = new Member(
			req.getEmail(), req.getPassword(), req.getName(),
			LocalDateTime.now());
		memberDao.insert(newMember);
		return newMember.getId();
	}
	
}

MemberRegisterService ν΄λž˜μŠ€λŠ” 이전 ν¬μŠ€νŒ…μ—λ„ μ½”λ“œκ°€ μ‘΄μž¬ν•©λ‹ˆλ‹€.

이 클래슀의 regist() λ©”μ„œλ“œλ₯Ό 보면 μ•žμ„œ μž‘μ„±ν•œ memberDao.selectByEmail() λ©”μ„œλ“œλ₯Ό μ΄μš©ν•΄μ„œ λ™μΌν•œ 이메일을 κ°–λŠ” νšŒμ› 데이터가 μ‘΄μž¬ν•˜λŠ”μ§€ ν™•μΈν•˜κ³ , μ‘΄μž¬ν•˜λ©΄ DuplicateMemberException을 λ°œμƒμ‹œν‚΅λ‹ˆλ‹€.


01.CHAP01/src/main/java/spring/RegisterRequest.java

package spring;

public class RegisterRequest {

	private String email;
	private String password;
	private String confirmPassword;
	private String name;
	
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getConfirmPassword() {
		return confirmPassword;
	}
	
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public boolean isPasswordEqualToConfirmPassword() {
		return password.equals(confirmPassword);
	}
	
}

RegisterRequest ν΄λž˜μŠ€λŠ” νšŒμ› κ°€μž…μ„ μ²˜λ¦¬ν•  λ•Œ ν•„μš”ν•œ 이메일, μ•”ν˜Έ, 이름 데이터λ₯Ό λ‹΄κ³  μžˆλŠ” κ°„λ‹¨ν•œ ν΄λž˜μŠ€μž…λ‹ˆλ‹€.


3. πŸ—οΈ μ•”ν˜Έ λ³€κ²½ κ΄€λ ¨ 클래슀

μ•”ν˜Έ λ³€κ²½ κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” ChangePasswordService ν΄λž˜μŠ€λŠ” μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.


01.CHAP01/src/main/java/spring/ChangePasswordService.java

package spring;

public class ChangePasswordService {

	private MemberDao memberDao;
	
	public void changePassword(String email, String oldPwd, String newPwd) {
		Member member = memberDao.selectByEmail(email);
		if(member == null)
			throw new MemberNotFoundException();
		
		member.changePassword(oldPwd, newPwd);
		
		memberDao.update(member);
	}

	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
	
}

ChangePasswordService ν΄λž˜μŠ€λŠ” μ•”ν˜Έλ₯Ό λ³€κ²½ν•  Member 데이터λ₯Ό μ°ΎκΈ° μœ„ν•΄ email을 μ‚¬μš©ν•©λ‹ˆλ‹€.

λ§Œμ•½ email에 ν•΄λ‹Ήν•˜λŠ” Memberκ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄ μ•„λž˜μ˜ λΆ€λΆ„μ—μ„œ μ˜ˆμ™Έλ₯Ό λ°œμƒμ‹œν‚΅λ‹ˆλ‹€.

if(member == null)
	throw new MemberNotFoundException();

Memberκ°€ μ‘΄μž¬ν•˜λ©΄ member.changePassword()λ₯Ό μ΄μš©ν•΄μ„œ μ•”ν˜Έλ₯Ό λ³€κ²½ν•˜κ³  update() λ©”μ„œλ“œλ₯Ό μ΄μš©ν•˜μ—¬ λ³€κ²½λœ 데이터λ₯Ό λ³΄κ΄€ν•©λ‹ˆλ‹€.


μ•„λž˜μ˜ setMemberDao() λ©”μ„œλ“œλ‘œ μ˜μ‘΄ν•˜λŠ” MemberDaoλ₯Ό μ „λ‹¬λ°›μŠ΅λ‹ˆλ‹€. 즉 Setterλ₯Ό ν†΅ν•΄μ„œ 의쑴 객체λ₯Ό μ£Όμž…λ°›μŠ΅λ‹ˆλ‹€.

public void setMemberDao(MemberDao memberDao) {
	this.memberDao = memberDao;
}

Member 클래슀의 changePassword() λ©”μ„œλ“œλŠ” λ‹€μŒ μ½”λ“œμ²˜λŸΌ oldPassword둜 μ „λ‹¬ν•œ μ•”ν˜Έκ°€ μΌμΉ˜ν•˜μ§€ μ•ŠμœΌλ©΄ μ˜ˆμ™Έλ₯Ό λ°œμƒμ‹œν‚€λ„λ‘ κ΅¬ν˜„ν–ˆμœΌλ―€λ‘œ

public void changePassword(String oldPassword, String newPassword) {
	if(!password.equals(oldPassword))
		throw new WrongIdPasswordException();
	this.password = newPassword;
}

μ•”ν˜Έκ°€ μΌμΉ˜ν•˜μ§€ μ•ŠμœΌλ©΄ ChangePasswordService 클래슀의

memberDao.update(member);

μ½”λ“œλŠ” μ‹€ν–‰λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.


ChangePasswordServiceκ°€ 이메일에 ν•΄λ‹Ήν•˜λŠ” Memberκ°€ μ‘΄μž¬ν•˜μ§€ μ•Šμ„ λ•Œ λ°œμƒμ‹œν‚€λŠ” MemberNotFoundException ν΄λž˜μŠ€λŠ” μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.


01.CHAP01/src/main/java/spring/MemberNotFoundException.java

package spring;

public class MemberNotFoundException extends RuntimeException {

}

이번 ν¬μŠ€νŒ…μ—μ„œλŠ” κ°„λ‹¨ν•œ ν”„λ‘œμ νŠΈ μ½”λ“œλ₯Ό μ§œλ³΄μ•˜μŠ΅λ‹ˆλ‹€.

λ‹€μŒ ν¬μŠ€νŒ…μ—μ„œλŠ” Assemblerλ₯Ό μ‚¬μš©ν•΄ 보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.

κ°μ‚¬ν•©λ‹ˆλ‹€.


πŸ’‘ Reference

https://www.kame.co.kr/nkm/detail.php?tcode=306&tbook_jong=3

profile
μ½”λ”©ν•˜λŠ” μˆ˜ν•™κ³Ό
post-custom-banner

0개의 λŒ“κΈ€