-
Notifications
You must be signed in to change notification settings - Fork 43
[3주차] 관심사 분리 1 - 신재희 #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yuntasha
wants to merge
24
commits into
Loopers-play-dev-lab:yuntasha
Choose a base branch
from
yuntasha:yuntasha
base: yuntasha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
9892844
:heavy_plus_sign: 리플렉션 cnrk
yuntasha ce97712
:sparkles: Car 추가
yuntasha dc5f9a4
:sparkles: 어노테이션 추가 (컴파일 이슈)
yuntasha 0eea9eb
:sparkles: 요구사항 3까지 구현
yuntasha 026cfae
:recycle: 이름 변경
yuntasha fc518f2
:heavy_plus_sign: assertThat가져오기
yuntasha 670a24d
:sparkles: getter 추가
yuntasha a68fe59
:sparkles: 요구사항 4 해결
yuntasha 4402540
:sparkles: 요구사항 5 해결
yuntasha d69fdfc
:sparkles: Lecture 요구사항 1, 2 해결
yuntasha 9104d4e
:sparkles: Lecture 요구사항 3 해결
yuntasha 081b01b
:sparkles: Lecture 요구사항 4 해결
yuntasha ba30f34
:sparkles: Lecture 요구사항 5 해결
yuntasha 8b136ae
:sparkles: Lecture 요구사항 6 해결
yuntasha c482f02
:sparkles: 컴포넌트 어노테이션 생성
yuntasha aeacaa9
:sparkles: Component 어노테이션 생성
yuntasha 08b15a4
:sparkles: 스캐너가 실행되도록 작성
yuntasha 2095347
:recycle: 빈 스토리지로 변경
yuntasha da39f2e
:sparkles: 컴포넌트 레포지토리 생성
yuntasha 39351a7
:sparkles: autowired 적용
yuntasha f1dcf50
:sparkles: 위상정렬을 통한 만드는 순서 조정 및 예외처리
yuntasha cc636ac
Merge pull request #4 from yuntasha/week3
yuntasha d0eacac
:recycle: Autowired 요구사항에 맞게 변경
yuntasha 7bb520a
Merge pull request #5 from yuntasha/week3-refactor
yuntasha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/diy/framework/beans/annotations/Autowired.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.diy.framework.beans.annotations; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target(ElementType.CONSTRUCTOR) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface Autowired { | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/diy/framework/beans/annotations/Component.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.diy.framework.beans.annotations; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target(ElementType.TYPE) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface Component { | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/diy/framework/beans/factory/BeanScanner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.diy.framework.beans.factory; | ||
|
|
||
| import com.diy.framework.beans.annotations.Autowired; | ||
| import org.reflections.Reflections; | ||
|
|
||
| import java.lang.annotation.Annotation; | ||
| import java.lang.reflect.Constructor; | ||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class BeanScanner { | ||
|
|
||
| private final Reflections reflections; | ||
|
|
||
| public BeanScanner(final String... basePackages) { | ||
| reflections = new Reflections(basePackages); | ||
| } | ||
|
|
||
| public Set<Class<?>> scanClassesTypeAnnotatedWith(final Class<? extends Annotation> annotation) { | ||
| return reflections.getTypesAnnotatedWith(annotation) | ||
| .stream() | ||
| .filter(type -> (!type.isAnnotation() && !type.isInterface())) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| public Constructor<?> scanConstructor(final Class<?> clazz) throws NoSuchMethodException { | ||
| Optional<Constructor<?>> autowired = Arrays.stream(clazz.getDeclaredConstructors()) | ||
| .filter(constructor -> Objects.nonNull(constructor.getDeclaredAnnotation(Autowired.class))) | ||
| .findAny(); | ||
| if (autowired.isPresent()) { | ||
| return autowired.get(); | ||
| } | ||
| return clazz.getDeclaredConstructor(); | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/diy/framework/beans/factory/BeanStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.diy.framework.beans.factory; | ||
|
|
||
| import com.diy.framework.beans.annotations.Component; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.util.*; | ||
|
|
||
| public class BeanStorage { | ||
|
|
||
| private static BeanStorage instance; | ||
|
|
||
| private final Map<String, Object> beans = new HashMap<>(); | ||
| private final BeanScanner bc = new BeanScanner("com.diy.app"); | ||
|
|
||
| private BeanStorage() { | ||
| Set<Class<?>> classes = bc.scanClassesTypeAnnotatedWith(Component.class); | ||
| makeBeans(classes); | ||
| } | ||
|
|
||
| public static BeanStorage getInstance() { | ||
| if (Objects.isNull(instance)) instance = new BeanStorage(); | ||
| return instance; | ||
| } | ||
|
|
||
| private void makeBeans(Set<Class<?>> classes) { | ||
| for (Class<?> clazz : classes) { | ||
| makeBean(clazz); | ||
| } | ||
| } | ||
|
|
||
| private Object makeBean(Class<?> clazz) { | ||
| Constructor<?> constructor = null; | ||
| try { | ||
| constructor = bc.scanConstructor(clazz); | ||
|
|
||
| Object[] params = Arrays.stream(constructor.getParameterTypes()) | ||
| .map(paramType -> findBean(paramType).orElseGet(() -> makeBean(paramType))) | ||
| .toArray(); | ||
|
|
||
| constructor.setAccessible(true); | ||
| Object bean = constructor.newInstance(params); | ||
| beans.put(clazz.getName(), bean); | ||
| return bean; | ||
| } catch (InstantiationException | IllegalAccessException | InvocationTargetException | | ||
| NoSuchMethodException e) { | ||
| throw new RuntimeException(e); | ||
| } finally { | ||
| if (constructor != null) { | ||
| constructor.setAccessible(false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Optional<Object> findBean(final Class<?> classType) { | ||
| return beans.values().stream() | ||
| .filter(classType::isInstance) | ||
| .findAny(); | ||
| } | ||
|
|
||
| public <T> Optional<T> getBean(final Class<T> classType) { | ||
| return beans.values().stream() | ||
| .filter(classType::isInstance) | ||
| .map(classType::cast) | ||
| .findAny(); | ||
| } | ||
| } | ||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package study.reflection; | ||
|
|
||
| public class Car { | ||
|
|
||
| private String name; | ||
| private int price; | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| public Car() { | ||
|
|
||
| } | ||
|
|
||
| public Car(String name, int price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| @PrintView | ||
| public void printView() { | ||
| System.out.println("자동차 정보를 출력 합니다."); | ||
| } | ||
|
|
||
| public String testGetName() { | ||
| return "test : " + name; | ||
| } | ||
|
|
||
| public String testGetPrice() { | ||
| return "test : " + price; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package study.reflection; | ||
|
|
||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface PrintView { | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
음.. 이걸 하드코딩 해도 괜찮을까요?