μ λ² ν¬μ€ν
μμλ DIμ κ΄νμ¬ κ³΅λΆν΄ 보μμ΅λλ€.
μ΄λ²μλ κ°λ¨ν νλ‘μ νΈλ₯Ό ν΅ν΄ Spring 곡λΆλ₯Ό ν΄λ³΄κ² μ΅λλ€.
μμ ν¬μ€ν
μ€ νλ‘μ νΈλ₯Ό import νλ ν¬μ€ν
μ΄ μμμ΅λλ€.
μ λ μ¬κΈ°μ importν νλ‘μ νΈλ₯Ό κ·Έλλ‘ μ¬μ©νμμ§λ§ μ λ°©λ²λλ‘ μ Projectλ₯Ό importν΄λ 무방ν©λλ€.
Maven νλ‘μ νΈλ₯Ό μμ±νκ³ Eclipseμ import νμκΈ° λλ¬Έμ λ€μ ν΄λμ€λ₯Ό μ°¨λ‘λλ‘ μμ±νκ² μ΅λλ€.
1. νμ λ°μ΄ν° κ΄λ ¨ ν΄λμ€
- Member
- WrongIdPasswordException
- MemberDao
2. νμ κ°μ μ²λ¦¬ κ΄λ ¨ ν΄λμ€
- DuplicateMemberException
- MemberRegisterService
- RegisterRequest
3. μνΈ λ³κ²½ κ΄λ ¨ ν΄λμ€
- MemberNotFoundException
- ChangePasswordService
κ°μ₯ λ¨Όμ μμ±ν μ½λλ 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);
}
}
νμ κ°μ
μ²λ¦¬μ νμν ν΄λμ€λ 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
ν΄λμ€λ νμ κ°μ
μ μ²λ¦¬ν λ νμν μ΄λ©μΌ, μνΈ, μ΄λ¦ λ°μ΄ν°λ₯Ό λ΄κ³ μλ κ°λ¨ν ν΄λμ€μ
λλ€.
μνΈ λ³κ²½ κΈ°λ₯μ μ 곡νλ 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λ₯Ό μ¬μ©ν΄ 보λλ‘ νκ² μ΅λλ€.
κ°μ¬ν©λλ€.
https://www.kame.co.kr/nkm/detail.php?tcode=306&tbook_jong=3