Skip to content
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 16 commits into from
Nov 7, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions nextstep-di/src/main/java/nextstep/di/factory/BeanFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,6 +28,34 @@ public <T> T getBean(Class<T> requiredType) {
}

public void initialize() {
for (Class<?> preInstanticateBean : preInstanticateBeans) {
beans.put(preInstanticateBean, createBean(preInstanticateBean));
Copy link

@kingbbode kingbbode Oct 31, 2019

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

이에 대한 테스트케이스도 작성이 되면 좋겠어요.

}
}

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());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예외를 추적하기 위한 정보가 e.getMessage() 면 충분할까요? 계획한 수준의 예외가 아니라면 Exception 상태를 보존해주면 좋을 것 같아요.

https://rules.sonarsource.com/java/tag/error-handling/RSPEC-1166
https://www.slipp.net/questions/350

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());
}
}