๐งก2022-08-10๐งก
https://blog.naver.com/writer0713/220700687650
EJBC (Enterprise Java Bean)
์๋ฐ ๊ฐ๋ฐ์๋ค์ด ํผ๊ณคํด์ง๊ธฐ ์์.. EJB๋ฅผ ๋ง๋ค๋ ๊ธฐ๋ณธ์ ์ธ ๊ฐ์ฒด์งํฅ ๊ฐ๋ ์ด ์๋ชป๋ ,. ํด๋์ค๋ฅผ ์๋ชป ๋ง๋ค์ด์
๋ณด์ ๊ด๋ จ EJB extend ์ฝํ๊ณ ์ฅํ.. ์ค์ค์ด ๋๋ ค์ค๋๊ฒ ๋ง์์ ๋ฌด๊ฑฐ์์ง -> ํด๋์ค ์ค๊ณ ์๋ชป
๊ฐ์ฒด์งํฅ ์ปจํผ๋ฐ์ค
POJO Plain Java Object
๊ฐ์ฒด์งํฅ ๊ฐ๋ ์ ์งํฅํ๋ฉด์ ์๋ฐ ์ค๋ธ์ ํธ์ ํน์ง์ ๊ฐ๊ณ ์๋.. ๊ทธ๋ด๋ฏํ๋๊น ํฌ์กฐ์ด๋์ด ์ผ์ด๋จ
๊ธฐ์ ๋ฉด์ ์์๋ ๋์ฌ ์ ์์. ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํ๋ ์์ํฌ์ ์ฐจ์ด๊ฐ ๋ญ๋,,,
๋ผ์ด๋ธ๋ฌ๋ฆฌ์๋ ๋์๋ ์ข๊ณ ํ๋ ์์ํฌ์์ ๋์๋ ์ข์
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ํน์ ํ ๊ธฐ๋ฅ๋ค ํจ์๋ ํด๋์ค๋ก ๋์ด๋ค ์ฐ๋ ๊ฒ
ex) ํ๊ทธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ JAR ํ์ผ..
ํ๋ ์์ํฌ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ชจ์ฌ์ ์๊พธ.. ํ ํธ์ธ ๊ฒ
์ด๋ค ๊ธฐ๋ฅ์ ์ฌ๊ธฐ๋ค๊ฐ ์ ์ด๋ผ ํ์ ๋ชป๋ฒ์ด๋จ.. ๋ฌผ๋ฆฌ์ ์ผ๋ก ๋ณด๋ฉด ํ๋ ์์ํฌ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์งํฉ์ฒด
ํฌ์กฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ ํ๋ ์์ํฌ๊ฐ ๋ฑ์ฅํ๋ค.
structs, spring ์ด ๋ฑ์ฅํ์๋ค ! !
์๊ฐ์ด ์ง๋๋ฉด์ ์คํ๋ง์ด ์์ฑ๋จ
Spring ์๋ฐ์ ๋ด์ ๋ง์ดํ๋ค.. ํด์ ์คํ๋ง
์คํ๋ง?? ์น ์ ํ๋ฆฌ์ผ์ด์ ๊ณผ ๋ฌด์จ ์๊ด์ด๋??
์คํ๋ง์ด๋ DI์ AOP๋ฅผ ์ง์ํ๋ POJO ๊ธฐ๋ฐ์ Framework์ด๋ค!
DI๊ฐ ๋ญ๊ณ AOP๊ฐ ๋ฌด์์ธ์ง ์์์ผ ์คํ๋ง์ ์ดํดํ ์ ์๋ค.
1. DI (Dependency Injection = ์์กด ์ฃผ์ )
์คํ๋ง์ด ์๋๊ณ ๊ฐ์ฒด์งํฅ ๋์์ธ๊ณผ ๊ด๋ จ๋์ด์์.
๋ฉ์ด๋ธ ํ๋ก์ ํธ pom ๋ฐ๊พธ๊ธฐ
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gyuone</groupId>
<artifactId>dISample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
https://mvnrepository.com/artifact/org.springframework/spring-context/5.3.22
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gyuone</groupId>
<artifactId>dISample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.22</version>
</dependency>
</dependencies>
</project>
๋๊ณ ์จ๊ฑฐ ํ์ธ
depends on ์์กด ๊ด๊ณ memberDao๋ MRS์ dependency..
memberRegisterService๋ memberDao๊ฐ ๊ผญ ๋ค์ด๊ฐ์ผํ๋ dependency
dependency.. ์์กด, ์์กด์ฑ
https://github.com/GyuWonLee/dIProject
com.gyuone.main
MainForAssembler.java
package com.gyuone.main;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.DuplicateFormatFlagsException;
import com.gyuone.common.MemberNotFoundException;
import com.gyuone.common.WrongIdPasswordException;
import com.gyuone.model.RegisterRequest;
import com.gyuone.service.Assembler;
import com.gyuone.service.ChangePasswordService;
import com.gyuone.service.MemberRegisterService;
public class MainForAssembler {
private static Assembler assembler = new Assembler();
public static void main(String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true) {
System.out.print("๋ช
๋ น์ด ์
๋ ฅ : ");
String command = reader.readLine();
if(command.equalsIgnoreCase("exit")) {
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
break;
}
if(command.startsWith("new ")) {
processNewCommand(command.split(" "));
continue;
}else if(command.startsWith("change ")) {
processChangeCommand(command.split(" "));
continue;
}
printHelp();
}
}
private static void printHelp() {
System.out.println();
System.out.println("์๋ชป๋ ๋ช
๋ น์
๋๋ค. ์๋ ๋ช
๋ น์ด ์ฌ์ฉ๋ฒ์ ํ์ธํ์ธ์.");
System.out.println("๋ช
๋ น์ด ์ฌ์ฉ๋ฒ:");
System.out.println("new ์ด๋ฉ์ผ ์ด๋ฆ ์ํธ ์ํธํ์ธ");
System.out.println("change ์ด๋ฉ์ผ ํ์ฌ๋น๋ฒ ๋ณ๊ฒฝ๋น๋ฒ");
System.out.println();
}
private static void processNewCommand(String[] arg) {
if(arg.length != 5) {
printHelp();
return;
}
MemberRegisterService regSvc = assembler.getMemberRegisterService();
RegisterRequest req = new RegisterRequest();
req.setEmail(arg[1]);
req.setName(arg[2]);
req.setPassword(arg[3]);
req.setConfirmPassword(arg[4]);
if(!req.isPasswordEqualToConfirmPassword()) {
System.out.println("์ํธ ํ์ธ์ด ๋ง์ง ์์ต๋๋ค.");
System.out.println();
return;
}
try {
regSvc.register(req);
System.out.println("๋ฑ๋ก ์๋ฃ");
System.out.println();
} catch (DuplicateFormatFlagsException e) {
// TODO: handle exception
System.out.println("์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฉ์ผ์
๋๋ค.");
System.out.println();
}
}
private static void processChangeCommand(String[] arg) {
if(arg.length != 4) {
printHelp();
return;
}
ChangePasswordService changePwdSvc = assembler.getChangePasswordService();
try {
changePwdSvc.changePassword(arg[1], arg[2], arg[3]);
System.out.println("์ํธ ๋ณ๊ฒฝ ์๋ฃ");
} catch (MemberNotFoundException e) {
// TODO: handle exception
System.out.println("์กด์ฌํ์ง ์๋ ์ด๋ฉ์ผ์
๋๋ค.");
System.out.println();
} catch (WrongIdPasswordException e) {
System.out.println("์ด๋ฉ์ผ๊ณผ ์ํธ ๋ถ์ผ์น");
System.out.println();
}
}
}
com.gyuone.common
DuplicateMemverException.java
package com.gyuone.common;
public class DuplicateMemberException extends RuntimeException{
public DuplicateMemberException(String message) {
super(message);
}
}
MemberNotFoundException.java
package com.gyuone.common;
public class MemberNotFoundException extends RuntimeException{
}
WrongIdPasswordException.java
package com.gyuone.common;
public class WrongIdPasswordException extends RuntimeException{
}
com.gyuone.dao
MemberDao.java
package com.gyuone.dao;
import java.util.HashMap;
import java.util.Map;
import com.gyuone.model.Member;
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);
}
}
com.gyuone.model
Member.java
package com.gyuone.model;
import java.time.LocalDateTime;
import com.gyuone.common.WrongIdPasswordException;
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 registerDateTime) {
super();
this.email = email;
this.password = password;
this.name = name;
this.registerDateTime = registerDateTime; // ์์ฑ์ ๋ง๋ค์ด์ฃผ๊ธฐ
}
public long getId() {
return id;
}
public 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;
}
}
RegisterRequest.java
package com.gyuone.model;
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);
}
}
com.gyuone.service
Assembler.java
package com.gyuone.service;
import com.gyuone.dao.MemberDao;
public class Assembler {
private MemberDao memberDao;
private MemberRegisterService regSvc;
private ChangePasswordService pwdSvc;
public Assembler() {
memberDao = new MemberDao();
regSvc = new MemberRegisterService(memberDao);
pwdSvc = new ChangePasswordService();
pwdSvc.setMemberDao(memberDao);
}
public MemberDao getMemberDao() {
return memberDao;
}
public MemberRegisterService getMemberRegisterService() {
return regSvc;
}
public ChangePasswordService getChangePasswordService() {
return pwdSvc;
}
}
ChangePasswordService.java
package com.gyuone.service;
import com.gyuone.common.MemberNotFoundException;
import com.gyuone.dao.MemberDao;
import com.gyuone.model.Member;
public class ChangePasswordService {
private MemberDao memberDao;
public void setMemberDao(MemberDao memberDao) {
this.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);
}
}
MemberRegisterService.java
package com.gyuone.service;
import java.time.LocalDateTime;
import com.gyuone.common.DuplicateMemberException;
import com.gyuone.dao.MemberDao;
import com.gyuone.model.Member;
import com.gyuone.model.RegisterRequest;
public class MemberRegisterService {
// private MemberDao memberDao = new MemberDao(); // ๊ฐํ ๊ฒฐํฉ
private MemberDao memberDao;
public MemberRegisterService(MemberDao memberDao) { // ์์ฑ์๋ฅผ ํตํด์ ์์กด์ ์ฃผ์
.. ์์ฑ์ ์์กด ์ฃผ์
= DI
this.memberDao = memberDao;
}
public long register(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();
}
}
MRS์ MDao๊ฐ๊ฐํ๊ฒ ๊ฒฐํฉ๋์ด ์๋ค. ๊ฐํ๊ฒ strongly coupled = > ๊ฒฐํฉ๋จ๊ณ๋ฅผ ์ฝํ๊ฒ ๋ง๋ค์ด์ผํ๋ค.
'IT > WEB' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[38์ผ์ฐจ] AOP/ aop concert project (0) | 2022.08.12 |
---|---|
[37์ผ์ฐจ] AutoDi (0) | 2022.08.11 |
[37์ผ์ฐจ] dISample project (0) | 2022.08.11 |
[37์ผ์ฐจ] Spring Bean (0) | 2022.08.11 |
[36์ผ์ฐจ] Servlet. jsp (0) | 2022.08.10 |
๋๊ธ