-
Notifications
You must be signed in to change notification settings - Fork 45
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
[DI - Step 1] 효오 미션 제출합니다. #11
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1cfc172
feat: BeanFactory initialize 구현
hyojaekim 00b44b2
feat: BeanFactory DI 구현
hyojaekim fb8709a
refactor: BeanFactory DI
hyojaekim b4870de
feat: BeanFactory getControllers 기능 추가
hyojaekim 6aa1b9b
feat: MVC 프레임워크 DI 적용
hyojaekim 8506e7f
refactor: ControllerScanner 제거
hyojaekim 091a1df
refactor: Slipp 모듈 DI 적용
hyojaekim 6050ab9
refactor: Exception logging 변경
vsh123 1bff5d4
refactor: UserDao 싱글턴 제거
vsh123 7217148
fix: Controller UserDao final 적용
vsh123 cea4809
refactor: BeanFactory, BeanScanner
vsh123 179e91b
fix: UserDao 싱글턴 제거에 따른 에러 수정
vsh123 11f986e
refactor: BeanFactory.createInstance()
vsh123 79ae8e4
feat: validateClassType 추가
hyojaekim c0e31ef
test: BeanFactory 불필요한 필드 및 메서드 제거
hyojaekim 30fd57c
test: BeanFactory 테스트 추가
hyojaekim 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 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 |
---|---|---|
|
@@ -4,6 +4,10 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
|
@@ -24,6 +28,34 @@ public <T> T getBean(Class<T> requiredType) { | |
} | ||
|
||
public void initialize() { | ||
for (Class<?> preInstanticateBean : preInstanticateBeans) { | ||
beans.put(preInstanticateBean, createBean(preInstanticateBean)); | ||
} | ||
} | ||
|
||
private Object createBean(Class<?> preInstanticateBean) { | ||
try { | ||
Constructor<?> injectedConstructor = BeanFactoryUtils.getInjectedConstructor(preInstanticateBean); | ||
if (injectedConstructor == null) { | ||
return createDefaultConstructorInstance(preInstanticateBean); | ||
} | ||
return createInstance(injectedConstructor); | ||
} catch (Exception e) { | ||
logger.error("### Bean create fail : {}", e.getMessage()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외를 추적하기 위한 정보가 https://rules.sonarsource.com/java/tag/error-handling/RSPEC-1166 |
||
throw new IllegalArgumentException("Bean create fail!"); | ||
} | ||
} | ||
|
||
private Object createDefaultConstructorInstance(Class<?> preInstanticateBean) throws IllegalAccessException, InstantiationException { | ||
return preInstanticateBean.newInstance(); | ||
} | ||
|
||
private Object createInstance(Constructor<?> injectedConstructor) throws IllegalAccessException, InvocationTargetException, InstantiationException { | ||
Class<?>[] parameterTypes = injectedConstructor.getParameterTypes(); | ||
List<Object> parameters = new ArrayList<>(); | ||
for (Class<?> parameterType : parameterTypes) { | ||
parameters.add(getBean(parameterType)); | ||
} | ||
return injectedConstructor.newInstance(parameters.toArray()); | ||
} | ||
} |
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.
(장황하게 작성하여 코멘트를 수정합니다!)
의도한 Bean 의 유형과 범위에서만 생성되고 있나요?
유형 : Controller, Service, Repository Annotation 이 작성된 class
범위 : package
이에 대한 테스트케이스도 작성이 되면 좋겠어요.