*본 프로젝트는 JSP 팀 프로젝트로 만들었던 Learning Management System을 Spring으로 이식하는 과정입니다.
**본 프로젝트는 김영한 선생님의 인프런 강의 스프링 핵심원리 - 기본편
을 바탕으로 실습하는 과정입니다. 스프링에 대해서 배우고 싶으시다면 이 강의를 들어보시는 것도 좋을 것입니다.
개발 환경
IDK : intelliJ
JDK : 자바 11
의존성은 김영한 선생님의 강의 순서대로 하기 위해 아무것도 넣지 않고 시작한다.
지금까지는 직접 Bean을 등록시키고 의존성을 생성자를 통해 직접 주입시켜주었다.
이제는 스프링의 기능을 활용하여 자동으로 스프링 빈을 등록하고, 의존관계 역시 자동으로 주입한다.
사용되는 어노테이션
@Component
@Component Scan
@Autowired
1. 구성 클래스 작성
Appconfig와 같이 구성을 담당하는 새로운 구성 클래스를 작성한다.
<AutoAppConfig>
1
2
3
4
5
6
7
8
9
10
11
12
|
package eleven.lms;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class))
//excludeFilter 는 기존의 Appconfig.java를 제외하고 컴포넌트를 스캔하기 위해 삽입했다. 기존 구성 클래스인 Appconfig를 삭제시 없어도 무방하다.
public class AutoAppconfig {
}
|
cs |
구성 클래스임을 알려주기 위해
@Configuration 어노테이션을 사용해주었으며, ComponentScan 어노테이션을 사용하여 컴포넌트 스캔을 사용한다.
2. @Component를 구현클래스들에 작성하기
3. 의존관계 주입을 위해 생성된 생성자에 @Autowired를 달고 의존관계 자동 주입
4. 테스트 코드 짜기
로직이 잘 작동하는지 테스트 코드를 짭니다.
1) memberA 는 08:56에 도착하여 16:00에 교실을 떠났습니다. -> 조퇴처리
2) memberB는 09:00에 도착하여 16:55에 교실을 떠났습니다. ->정상출석처리
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
|
@Test
void createAttend2() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
Date start = dateFormat.parse("08:56:00");
Date end = dateFormat.parse("16:00:00");
Date start2 = dateFormat.parse("09:00:00");
Date end2 = dateFormat.parse("16:55:00");
MemberAttend memberAttend1 = new MemberAttend("memberA",start,end);
MemberAttend memberAttend2 = new MemberAttend("memberB",start2,end2);
ApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppconfig.class);
//AttendPolicy attendPolicy = ac.getBean(AttendPolicy.class);
AttendService attendService = ac.getBean(AttendService.class);
AttendRepository attendRepository = ac.getBean(AttendRepository.class);
attendService.CreateAttend(memberAttend1);
attendRepository.save(memberAttend1);
attendService.CreateAttend(memberAttend2);
attendRepository.save(memberAttend2);
MemberAttend MemberA = attendRepository.findById("memberA");
System.out.println("memberAttend1 = " + memberAttend1);
MemberAttend MemberB = attendRepository.findById("memberB");
System.out.println("memberAttend2 = " + memberAttend2);
Assertions.assertThat(MemberA).isNotEqualTo(MemberB);
}
}
|
cs |
<실행결과>
따로 분리해서 저장이 되고 잘 불러와 짐을 알 수 있습니다.
'프로그래밍 언어 > Spring' 카테고리의 다른 글
<JPA> JPA 1. 내부 동작 방식 영속성 (0) | 2021.11.18 |
---|---|
<Spring> 스프링 핵심원리 - 기본편 후기 (0) | 2021.11.17 |
<IntelliJ>Git Hub 연동하기 (0) | 2021.11.03 |
<Spring>LMS프로젝트5. 학생 출결 로직 작성(2) 스프링으로 이식 (0) | 2021.10.31 |
<Spring>LMS프로젝트4. 학생 출결 로직 작성(2)OCP,DIP 준수하게 수정 (0) | 2021.10.29 |