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

[37์ผ์ฐจ] AutoDi

by GWLEE 2022. 8. 11.

๐Ÿ’›2022-08-11๐Ÿ’›

 

 

common

DuplicateMemberException.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{
	
}

 

config

AppConfig1.java

package com.gyuone.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.gyuone.dao.MemberDao;
import com.gyuone.service.MemberPrinter;


@Configuration
public class AppConf1 {
	
	@Bean
	public MemberDao memberDao() {
		return new MemberDao();
	}
	
	@Bean
	public MemberPrinter memberPrinter() {
		return new MemberPrinter();
	}

}

 

AppConfig2.java

package com.gyuone.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.gyuone.dao.MemberDao;
import com.gyuone.service.ChangePasswordService;
import com.gyuone.service.MemberInfoPrinter;
import com.gyuone.service.MemberListPrinter;
import com.gyuone.service.MemberPrinter;
import com.gyuone.service.MemberRegisterService;
import com.gyuone.service.VersionPrinter;

@Configuration
public class AppConf2 {
	
	@Autowired
	private MemberDao memberDao;
	@Autowired
	private MemberPrinter memberPrinter;

	@Bean
	public MemberRegisterService memberRegSvc() { // MemberRegisterService์˜ ์‹๋ณ„์ž๋Š” memberRegSvc
		return new MemberRegisterService(memberDao);
	}

	@Bean
	public ChangePasswordService changePwdSvc() {
		ChangePasswordService pwdSvc = new ChangePasswordService();
		pwdSvc.setMemberDao(memberDao);
		return pwdSvc;
	}

	@Bean
	public MemberListPrinter listPrinter() {
		return new MemberListPrinter(memberDao, memberPrinter);
	}

	@Bean
	public MemberInfoPrinter infoPrinter() {
		MemberInfoPrinter infoPrinter = new MemberInfoPrinter();
		infoPrinter.setMemberDao(memberDao);
		infoPrinter.setPrinter(memberPrinter);
		return infoPrinter;
	}

	@Bean
	public VersionPrinter versionPrinter() {
		VersionPrinter versionPrinter = new VersionPrinter();
		versionPrinter.setMajorVersion(5);
		versionPrinter.setMinorVersion(0);
		return versionPrinter;
	}

}

 

AppConfig

package com.gyuone.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.gyuone.dao.MemberDao;
import com.gyuone.service.MemberPrinter;

@Configuration
@Import({AppConf2.class})
public class AppConfImport {
	
	@Bean
	public MemberDao memberDao() {
		return new MemberDao();
	}
	
	@Bean
	public MemberPrinter memberPrinter() {
		return new MemberPrinter();
	}
	
	
	
	
}

 

 

AppCtx.java

package com.gyuone.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

import com.gyuone.dao.MemberDao;
import com.gyuone.service.ChangePasswordService;
import com.gyuone.service.MemberInfoPrinter;
import com.gyuone.service.MemberListPrinter;
import com.gyuone.service.MemberPrinter;
import com.gyuone.service.MemberRegisterService;
import com.gyuone.service.MemberSummaryPrinter;
import com.gyuone.service.VersionPrinter;

@Configuration
@ComponentScan(basePackages = {"com.gyuone"},
excludeFilters = @ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE, value = {AppConf1.class, AppConf2.class, AppConfImport.class })
) // ์ง€์šฐ์ง€ ์•Š๊ณ  ์—†์• ๋ฒ„๋ฆฌ๊ธฐ ํฌํ•จ์‹œํ‚ค์ง€ ์•Š์Œ
public class AppCtx {

//	@Bean
//	public MemberDao memberDao() {
//		return new MemberDao();
//	}
//
//	@Bean
//	public MemberRegisterService memberRegSvc() { // MemberRegisterService์˜ ์‹๋ณ„์ž๋Š” memberRegSvc
////		return new MemberRegisterService(memberDao());
//		return new MemberRegisterService();
//	}
//	
//	@Bean
//	public ChangePasswordService changePwdSvc() {
//		ChangePasswordService pwdSvc = new ChangePasswordService();
////		pwdSvc.setMemberDao(memberDao()); ํ•„๋“œ์—๋‹ค๊ฐ€ Autowired ์ ์šฉํ•˜๋ฉด ์•ˆํ•ด๋„ ๋œ๋‹ค.
//		return pwdSvc;
//	}
//	
//	@Bean
//	public MemberPrinter memberPrinter() {
//		return new MemberPrinter();
//	}
	
	@Bean
	@Qualifier("printer")
	public MemberPrinter memberPrinter1() {
		return new MemberPrinter();
	}
	
	@Bean
	@Qualifier("summaryPrinter")
	public MemberPrinter memberPrinter2() {
		return new MemberSummaryPrinter();
	}
	
//	@Bean
//	public MemberListPrinter listPrinter() {
////		return new MemberListPrinter(memberDao(), memberPrinter());
//		return new MemberListPrinter();
//	}
	
//	@Bean
//	public MemberInfoPrinter infoPrinter() {
//		MemberInfoPrinter infoPrinter = new MemberInfoPrinter();
////		infoPrinter.setMemberDao(memberDao());
//		infoPrinter.setPrinter(memberPrinter2()); //spring์ด ์•Œ์•„์„œ ํ˜ธ์ถœ autowired
//		return infoPrinter;
//	}
	
	@Bean
	public VersionPrinter versionPrinter() {
		VersionPrinter versionPrinter = new VersionPrinter();
		versionPrinter.setMajorVersion(5);
		versionPrinter.setMinorVersion(0);
		return versionPrinter;
	}
	
}

 

 

 

dao

MemberDao.java

package com.gyuone.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.gyuone.model.Member;

@Component
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);
	}
	
	public Collection<Member> selectAll(){
		return map.values();
	}
}

 

 

main

 

MainForSpring.java

package com.gyuone.main;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.DuplicateFormatFlagsException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.gyuone.common.DuplicateMemberException;
import com.gyuone.common.MemberNotFoundException;
import com.gyuone.common.WrongIdPasswordException;
import com.gyuone.config.AppCtx;
import com.gyuone.model.RegisterRequest;
import com.gyuone.service.ChangePasswordService;
import com.gyuone.service.MemberInfoPrinter;
import com.gyuone.service.MemberListPrinter;
import com.gyuone.service.MemberRegisterService;
import com.gyuone.service.VersionPrinter;

public class MainForSpring {
	private static ApplicationContext ctx = null;
	
	public static void main(String[] args) throws Exception{
		ctx = new AnnotationConfigApplicationContext(AppCtx.class);
		
		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;
			}else if(command.startsWith("list")){
				processListCommand();
				continue;
			}else if(command.startsWith("info")) {
				processInfoCommand(command.split(" "));
				continue;
			}else if(command.startsWith("version")) {
				processVersionCommand();
				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 = ctx.getBean(MemberRegisterService.class);
		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 (DuplicateMemberException e) {
			// TODO: handle exception
			System.out.println("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค.");
			System.out.println();
		}
	}
	
	private static void processChangeCommand(String[] arg) {
		if(arg.length != 4) {
			printHelp();
			return;
		}
		ChangePasswordService changePwdSvc = ctx.getBean(ChangePasswordService.class);
		try {
			changePwdSvc.changePassword(arg[1], arg[2], arg[3]);
			System.out.println("์•”ํ˜ธ ๋ณ€๊ฒฝ ์™„๋ฃŒ");
			System.out.println();
		} catch (MemberNotFoundException e) {
			// TODO: handle exception
			System.out.println("์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค.");
			System.out.println();
		} catch (WrongIdPasswordException e) {
			System.out.println("์ด๋ฉ”์ผ๊ณผ ์•”ํ˜ธ ๋ถˆ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค.");
			System.out.println();
		}
	}
	
	private static void processListCommand() {
		MemberListPrinter listPrinter = ctx.getBean("listPrinter", MemberListPrinter.class); // "listPrinter", MemberListPrinter.class๊ฐ€ ์‹๋ณ„์ž, ํƒ€์ž…
		listPrinter.printAll();
	}
	
	private static void processInfoCommand(String[] arg) {
		if(arg.length != 2) {
			printHelp();
			return;
		}
		MemberInfoPrinter infoPrinter = ctx.getBean("infoPrinter", MemberInfoPrinter.class);
		infoPrinter.printMemberInfo(arg[1]);
	}
	
	private static void processVersionCommand() {
		VersionPrinter versionPrinter = ctx.getBean("versionPrinter", VersionPrinter.class);
		versionPrinter.print();
	}
}

 

 

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);
	}
	

}

 

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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gyuone.common.MemberNotFoundException;
import com.gyuone.dao.MemberDao;
import com.gyuone.model.Member;

@Component
public class ChangePasswordService {
	
	@Autowired
	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);
	}
}

 

MemberInfoPrinter.java

package com.gyuone.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.gyuone.dao.MemberDao;
import com.gyuone.model.Member;

@Component("infoPrinter") // ์†Œ๋ฌธ์ž๋ฅผ ๋ฐ”๊พผ์ด๋ฆ„์œผ๋กœ ์ฃผ๊ฒŒ ๋˜๋Š”๋ฐ ํŠน๋ณ„ํ•œ ์ด๋ฆ„์œผ๋กœ ์ง€์ •ํ•ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์‹๋ณ„์ž๋ฅผ ์ค€๋‹ค. ์ค€ ์ด๋ฆ„์œผ๋กœ ๋“ฑ๋กํ•จ.
public class MemberInfoPrinter {
	private MemberDao memberDao;
	private MemberPrinter printer;
	
	@Autowired
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
	
	@Autowired
	@Qualifier("printer")
	public void setPrinter(MemberPrinter printer) {
		this.printer = printer;
	}
	
	public void printMemberInfo(String email) {
		Member member = memberDao.selectByEmail(email);
		if (member == null) {
			System.out.println("๋ฐ์ดํ„ฐ ์—†์Œ\n");
			return;
		}
		printer.print(member);
		System.out.println();
	}
}

 

MemberListPrinter.java

package com.gyuone.service;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.gyuone.dao.MemberDao;
import com.gyuone.model.Member;

@Component("listPrinter")
public class MemberListPrinter {
	private MemberDao memberDao;
	private MemberPrinter printer;
	
	public MemberListPrinter() {

	}
	public MemberListPrinter(MemberDao memberDao, MemberPrinter printer) {
		this.memberDao = memberDao;
		this.printer = printer;
	}
	
	public void printMemberInfo(String email) {
		Member member = memberDao.selectByEmail(email);
		if(member == null) {
			System.out.println("๋ฐ์ดํ„ฐ ์—†์Œ\n");
			return;
		}
		printer.print(member);
		System.out.println();
	}


	public void printAll() {
		Collection<Member> members = memberDao.selectAll();
		members.forEach(m -> printer.print(m)); 
	}

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

	@Autowired
	@Qualifier("summaryPrinter")
	public void setPrinter(MemberPrinter printer) {
		this.printer = printer;
	}
	
	
	

	
}

 

MemberPrinter.java

package com.gyuone.service;

import java.time.format.DateTimeFormatter;

import org.springframework.beans.factory.annotation.Autowired;

import com.gyuone.model.Member;

public class MemberPrinter {
	private DateTimeFormatter dateTimeFormatter;
	
	public MemberPrinter() {
		dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy๋…„ MM์›” dd์ผ");
	}
	
	public void print(Member member) {
		if (dateTimeFormatter == null) {
			System.out.printf("ํšŒ์›์ •๋ณด : ์•„์ด๋””=%d, ์ด๋ฉ”์ผ=%s, ์ด๋ฆ„=%s, ๋“ฑ๋ก์ผ=%tF\n", member.getId(),
					member.getEmail(), member.getName(), member.getRegisterDateTime());
		} else {
			System.out.printf("ํšŒ์›์ •๋ณด : ์•„์ด๋””=%d, ์ด๋ฉ”์ผ=%s, ์ด๋ฆ„=%s, ๋“ฑ๋ก์ผ=%s\n", member.getId(),
					member.getEmail(), member.getName(), 
					dateTimeFormatter.format(member.getRegisterDateTime()));
		}
	}

	@Autowired(required = false) // ํ•„์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. default๋Š” true
	public void setDateTimeFormatter(DateTimeFormatter dateTimeFormatter) {
		this.dateTimeFormatter = dateTimeFormatter;
	}

	
	
	
	
}

 

MemberRegisterService.java

package com.gyuone.service;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gyuone.common.DuplicateMemberException;
import com.gyuone.dao.MemberDao;
import com.gyuone.model.Member;
import com.gyuone.model.RegisterRequest;

@Component
public class MemberRegisterService {
//	private MemberDao memberDao = new MemberDao(); // ๊ฐ•ํ•œ ๊ฒฐํ•ฉ
	
	@Autowired
	private MemberDao memberDao;
	
	public MemberRegisterService() { // 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();
	}
}

 

MemberSummaryPrinter.java

package com.gyuone.service;

import com.gyuone.model.Member;

public class MemberSummaryPrinter extends MemberPrinter {

	@Override
	public void print(Member member) {
		// TODO Auto-generated method stub
		System.out.printf("ํšŒ์›์ •๋ณด : ์ด๋ฉ”์ผ=%s, ์ด๋ฆ„=%s\n", member.getEmail(), member.getName());
	}

}

 

 

VersionPrinter.java

package com.gyuone.service;

public class VersionPrinter {
	private int majorVersion;
	private int minorVersion;
	
	public void print() {
		System.out.printf("์ด ํ”„๋กœ๊ทธ๋žจ์˜ ๋ฒ„์ „์€ %d.%d์ž…๋‹ˆ๋‹ค.\n\n", majorVersion, minorVersion);
	}

	public void setMajorVersion(int majorVersion) {
		this.majorVersion = majorVersion;
	}

	public void setMinorVersion(int minorVersion) {
		this.minorVersion = minorVersion;
	}
	
}

 

 

 

'IT > WEB' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[38์ผ์ฐจ] FirstSpring  (0) 2022.08.12
[38์ผ์ฐจ] AOP/ aop concert project  (0) 2022.08.12
[37์ผ์ฐจ] dISample project  (0) 2022.08.11
[37์ผ์ฐจ] Spring Bean  (0) 2022.08.11
[36์ผ์ฐจ] ์Šคํ”„๋ง  (0) 2022.08.10

๋Œ“๊ธ€