프로그래밍 언어/Spring

<Spring>LMS프로젝트5. 학생 출결 로직 작성(2) 스프링으로 이식

창조적생각 2021. 10. 31. 14:57

 

*본 프로젝트는 JSP 팀 프로젝트로 만들었던 Learning Management System을 Spring으로 이식하는 과정입니다.

**본 프로젝트는 김영한 선생님의 인프런 강의 스프링 핵심원리 - 기본편

 

스프링 핵심 원리 - 기본편 - 인프런 | 강의

스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다., 스프링 핵심 원리를 이해하고, 성장하는 개발자가 되어보세요! 📣 확인해주

www.inflearn.com

을 바탕으로 실습하는 과정입니다. 스프링에 대해서 배우고 싶으시다면 이 강의를 들어보시는 것도 좋을 것입니다.

 

개발 환경

IDK : intelliJ

JDK : 자바 11

의존성은 김영한 선생님의 강의 순서대로 하기 위해 아무것도 넣지 않고 시작한다.

 

이전까지는 순수한 자바만을 이용하여 코드를 작성했다.

이제 스프링 프레임워크의 기능을 사용하기 위해 스프링 프레임 워크의 빈팩토리에 등록하고 사용하는 방법에 대해서 알아본다.

스프링은 만들어진 객체를 어노테이션을 통해 자동으로 BeanFactory에 저장해놓고 불러올 수 있는 기능을 제공한다.

<Class Appconfig>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package eleven.lms;
 
import eleven.lms.attend.AttendRepository;
import eleven.lms.attend.AttendService;
import eleven.lms.attend.AttendServiceImpl;
import eleven.lms.attend.MemoryAttendRepository;
import eleven.lms.attendrule.AttendPolicy;
import eleven.lms.attendrule.OneDayPolicy;
import eleven.lms.member.MemberRepository;
import eleven.lms.member.MemberService;
import eleven.lms.member.MemberServiceImpl;
import eleven.lms.member.MemoryMemberRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.text.ParseException;
 
@Configuration
public class AppConfig {
 
    @Bean
    public MemberService memberService(){//MemberService의 구현체 선택
        return new MemberServiceImpl(memberRepository());
    }
 
    @Bean
    public MemberRepository memberRepository(){// 위에서 사용될 memberRepository의 구현체를 선책해준다.
        return new MemoryMemberRepository();
    }
 
    @Bean
    public AttendRepository attendRepository(){
        return new MemoryAttendRepository();
    }
 
    @Bean
    public AttendService attendService() throws ParseException {
        return new AttendServiceImpl(attendPolicy());
    }
 
    @Bean
    public AttendPolicy attendPolicy() throws ParseException {
        return new OneDayPolicy();
    }
 
}
cs

1. 어플리케이션의 구성을 담당하는 클래스 Appconfig 클래스에 @Configuration 어노테이션을 붙여줌으로써 어플리케이션의 구성을 담당하는 클래스라는 것을 표시하고, 안에 구현된 객체들 위에 @Bean 어노테이션을 붙여줌으로써 빈팩토리에 빈으로 등록되게 합니다.

 

<Class AttendApp>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package eleven.lms.attend;
 
import eleven.lms.AppConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class AttendApp {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
        Date start = dateFormat.parse("08:56:00");
        Date end = dateFormat.parse("16:00:00");
 
        //        AttendService attendService = new AttendServiceImpl();
        //        AttendRepository attendRepository = new MemoryAttendRepository();
 
        MemberAttend memberAttend = new MemberAttend("MemberA",start,end);
 
//        AppConfig appConfig = new AppConfig();
//        AttendService attendService = appConfig.attendService();
//        AttendRepository attendRepository = appConfig.attendRepository();
 
       ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
       AttendService attendService = applicationContext.getBean("attendService",AttendService.class);
       AttendRepository attendRepository = applicationContext.getBean("attendRepository",AttendRepository.class);
 
        attendService.CreateAttend(memberAttend);
        attendRepository.save(memberAttend);
 
        System.out.println(memberAttend);
    }
}
cs

2.기존의 AppConfig에서 직접가져오는 것이 아니라 스프링프레임워크의 빈팩토리에 등록되어 있는 빈객체들을 빈팩토리에서 모든 것을 상속받은 ApplicationContext에서 객체들을 가져옵니다.

 

이제 실행을 해보면

결과값은 기존과 같이

MemberAttend{memberId='MemberA', attendStandard=earlyLeave, inTime=Thu Jan 01 08:56:00 KST 1970, outTime=Thu Jan 01 16:00:00 KST 1970}

로 나타나지만 기존에 보지 못한 것들이 위에 뜨는 것을 볼수 있습니다. 이들을 잘 살펴보면

빈 팩토리에 빈으로 구현체들의 인스턴스가 생성되었다는 것을 볼 수 있습니다.

 

 

728x90