๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT/WEB

[36์ผ์ฐจ] ์Šคํ”„๋ง

by GWLEE 2022. 8. 10.

 

 

๐Ÿงก2022-08-10๐Ÿงก

 

 

https://blog.naver.com/writer0713/220700687650

 

 

 

[java] POJO๋ž€?

์ž๋ฐ”์™€ ์Šคํ”„๋ง ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด์„œ ์—ฌ๋Ÿฌ ์ž๋ฃŒ๋ฅผ ์ฐพ์•„๋ณด๋Š”๋ฐ ํ•ญ์ƒ POJO๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ๋งŽ์ด ๋“ฑ์žฅํ–ˆ๋‹ค. ๋„๋Œ€์ฒด P...

blog.naver.com

 

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

 

GitHub - GyuWonLee/dIProject

Contribute to GyuWonLee/dIProject development by creating an account on GitHub.

github.com

 

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

๋Œ“๊ธ€