Today I learned

vics·2020년 12월 12일

강의 자료 출처(인프런 김영한 강사님)

오늘 하루 배운 것을 정리하기 위한 용도의 글입니다. 잘못된 부분에 대한 댓글은 언제든 환영입니다 :)

1. Work flow

  • Make MemberRepository(interface)

  • MemoryMemberRepository(Implementation)

  • MemberService(detailed business logic of the Repository class)

  • PostMapping & Key field inside POJO class

2. Make Test case,

ClassName +

3. make detailed business logic inside

private void invalidateDuplicateMember(Member member) {
                .ifPresent(member1 -> {
                    throw new IllegalStateException("이미 존재하는 회원입니다.");

4. How to register spring bean

4-1. Component Scan & AutoWired

Annotation @Controller, @Service, @Repository and @AutoWired for the constructor ensures that Spring Container contains them as components when the app starts.

public class MemberController {

    private final MemberService memberService;

    @Autowired // D.I of MemberService from the spring container.
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
@Repository // register this Repository in the spring container.
public class MemoryMemberRepository implements MemberRepository {
    private static MemoryMemberRepository memoryMemberRepository;

    public static MemoryMemberRepository getInstance() {
        if (memoryMemberRepository == null) {
            memoryMemberRepository = new MemoryMemberRepository();
        return memoryMemberRepository;
@Service // register this service in the spring container.
public class MemberService {

    private final MemoryMemberRepository memberRepository;

    @Autowired // D.I of Repository from the spring container.
    public MemberService(MemoryMemberRepository memberRepository) { // D.I
        this.memberRepository = memberRepository;

4-2. register bean using config class

It works the same with <4-1>way. This is how you register all beans in one configuration file.

public class SpringConfig {

    public MemberService memberService(){
        return new MemberService(memberRepository());

    public MemberRepository memberRepository() {
        return new MemoryMemberRepository();

5. PostMapping

    public String create(MemberForm form) {
        Member member = new Member();


        return "redirect:/";
  • receives parameter 'MemberForm' type.
public class MemberForm {

    private String name;

    public String getName() {
        return name;

    public void setName(String name) { = name;

  • name property is automatically set when post method is executed. Because it matches name field in HTML.
<html xmlns:th="">

<div class="container">

    <form action="/members/new" method="post">
        <div class="form-group">
            <label for="name">이름</label>
            <input type="text" id="name" name="name" placeholder="이름을 입력하세요"> <!--
            name field as a key which is same with the property in MemberForm class.
        <button type="submit">등록</button>
</div> <!-- /container -->

  • make sure name field of input tag matches the one inside MemberForm POJO class.
No pleasure, No gain

0개의 댓글