본문 주요 내용 및 무엇 주로 spring 의 ioc 에 대해 debug 방식 으로 spring 이 bean 을 어떻게 만 들 고 가 져 오 는 지 설명 합 니 다. 이전 블 로그 에서 spring 소스 코드 를 분석 하고 IOC 에서 bean 자원 설정 파일 을 불 러 옵 니 다. test 코드 는:
public class test {
public static void main(String[] args) {
ApplicationContext cx = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonServiceImpl obj = cx.getBean(PersonServiceImpl.class);
System.out.println(obj.toString());
}
}
getBean 방법 은 bean 대상 을 얻 고 그 내부 에 들 어가 그 원 리 를 파악 합 니 다. 아래 debug 는 PersonServiceImpl obj = cx. getBean (PersonServiceImpl. class) 에 들 어 갑 니 다.이 줄 의 코드.
1. 입장 cx. getBean () 방법:
public T getBean(Class requiredType) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(requiredType);
}
넘 어 갔 어. ClassPathXmlApplication Context 류 의 부모 클래스 Abstract Application Context 에서 Abstract Application Context 는 추상 적 인 클래스 이 고 Abstract Application Context 는 여러 개의 재 업로드 getBean () 방법 이 있 으 며 그 중에서 가장 익숙 한 것 은 getBean (String name) 과 getBean (Class required Type) 으로 각각 bean 의 id 와 bean 대상 류 의 class 대상 으로 전 달 됩 니 다.Abstract Application Context 추상 류 간접 적 으로 계승 되 었 습 니 다. BeanFactory 인터페이스, BeanFactory 는 spring ioc 의 원조 라 고 할 수 있 으 며 모든 인터페이스, 추상 류, 일반 류 가 계승 되 었 다. BeanFactory 는 자바 의 Object 류 에 해당 하 며, BeanFactory 인터페이스 에서 정의 하 는 방법 은 많 지 않 으 며, 주로 bean 에 대한 조작 으로 getBean (), contains Bean (), isSingleton () 등 이 있다.
2、 getBeanFactory (). getBean (required Type) 은 먼저 getBeanFactory () 에서 만 든 BeanFactory 를 가 져 옵 니 다. 존재 하지 않 거나 닫 히 지 않 으 면 이상 을 던 집 니 다. getBean 방법 에 들 어가 서 Default Listable BeanFactory 클래스 로 이동 합 니 다.
public T getBean(Class requiredType) throws BeansException {
Assert.notNull(requiredType, "Required type must not be null");
String[] beanNames = getBeanNamesForType(requiredType);
if (beanNames.length > 1) {
ArrayList autowireCandidates = new ArrayList();
for (String beanName : beanNames) {
if (getBeanDefinition(beanName).isAutowireCandidate()) {
autowireCandidates.add(beanName);
}
}
if (autowireCandidates.size() > 0) {
beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
}
}
if (beanNames.length == 1) {
return getBean(beanNames[0], requiredType);
}
else if (beanNames.length > 1) {
T primaryBean = null;
for (String beanName : beanNames) {
T beanInstance = getBean(beanName, requiredType);
if (isPrimary(beanName, beanInstance)) {
if (primaryBean != null) {
throw new NoUniqueBeanDefinitionException(requiredType, beanNames.length,
"more than one 'primary' bean found of required type: " + Arrays.asList(beanNames));
}
primaryBean = beanInstance;
}
}
if (primaryBean != null) {
return primaryBean;
}
throw new NoUniqueBeanDefinitionException(requiredType, beanNames);
}
else if (getParentBeanFactory() != null) {
return getParentBeanFactory().getBean(requiredType);
}
else {
throw new NoSuchBeanDefinitionException(requiredType);
}
}
방법. getBeanNames ForType (required Type) 은 클래스 유형 에 이미 존재 하 는 bean 의 이름 집합 을 되 돌려 줍 니 다. bean 의 이름 은 id 입 니 다. getBeanNames ForType () 에 들 어 가 는 방법 은 spring 이 Array List 를 사용 하여 bean 이름 집합 을 저장 하 는 것 을 볼 수 있 습 니 다. 여기 서 우 리 는 하나의 bean 만 지정 하 였 기 때문에 beanNames 의 값 도 하나 뿐 입 니 다. beanNames. length 의 값 은 1 입 니 다.
3. getBean (beanNames [0], required Type) 에 들 어가 AbstractBean Factory 로 이동 추상 클래스 중:
public T getBean(String name, Class requiredType) throws BeansException {
return doGetBean(name, requiredType, null, false);
}
더욱 들어간다
protected T doGetBean(
final String name, final Class requiredType, final Object[] args, boolean typeCheckOnly)
throws BeansException {
final String beanName = transformedBeanName(name);
Object bean;
// Eagerly check singleton cache for manually registered singletons.
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
if (logger.isDebugEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
}
}
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// Fail if we're already creating this bean instance:
// We're assumably within a circular reference.
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// Check if bean definition exists in this factory.
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.
String nameToLookup = originalBeanName(name);
if (args != null) {
// Delegation to parent with explicit args.
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else {
// No args -> delegate to standard getBean method.
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
}
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on.
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dependsOnBean : dependsOn) {
if (isDependent(beanName, dependsOnBean)) {
throw new BeanCreationException("Circular depends-on relationship between '" +
beanName + "' and '" + dependsOnBean + "'");
}
registerDependentBean(dependsOnBean, beanName);
getBean(dependsOnBean);
}
}
// Create bean instance.
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, new ObjectFactory
4. 먼저 본다 Object shared Instance = getSingleton (beanName) 방법:
public Object getSingleton(String beanName) {
return getSingleton(beanName, true);
}
/**
* Return the (raw) singleton object registered under the given name.
*
Checks already instantiated singletons and also allows for an early
* reference to a currently created singleton (resolving a circular reference).
*/
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
/** Cache of singleton objects: bean name --> bean instance */
private final Map singletonObjects = new ConcurrentHashMap(64);
주 해 는 주어진 bean 이름 을 사용 하여 원생 의 singleton 인 스 턴 스 를 되 돌려 줍 니 다.우선 주어진 이름 에 따라 singleton Objects 에서 Object 를 가 져 옵 니 다. singleton Objects 는 Concurrent HashMap 으로 bean 인 스 턴 스 를 저장 하 는 데 사 용 됩 니 다. 캐 시 라 고 할 수 있 습 니 다.캐 시 bean 대상 이 없 기 때문에 singleton Object 가 비어 있 습 니 다.isSingleton Currently InCreation () 은 현재 singleton 인 스 턴 스 가 만 들 고 있 는 과정 인지 판단 하 는 데 사 용 됩 니 다. 이 때 는 false 로 되 돌아 가 야 합 니 다. 만 들 고 있 지 않 기 때 문 입 니 다. if (singleton Object = = null & & isSingleton Currently InCreation (beanName) 값 은 false 이 고 getSingleton () 방법 은 비어 있 습 니 다.
5. 다시 3 의 getBean () 방법 으로 돌아 가면 shared Instance 가 비어 있 고 debug 는 else 코드 블록 에 들 어간 다음 에 들 어 갑 니 다.
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
markBeanAsCreated () 에 들 어 가 는 방법:
/**
* Mark the specified bean as already created (or about to be created).
*
This allows the bean factory to optimize its caching for repeated
* creation of the specified bean.
* @param beanName the name of the bean
*/
protected void markBeanAsCreated(String beanName) {
this.alreadyCreated.add(beanName);
}
설명 은 지정 한 bean 표 지 를 이미 만 들 었 거나 곧 만 들 것 입 니 다. already Created 역시 hashmap 이 고 key 는 bean 의 이름 입 니 다. value 는 Boolean 이 고 spring 은 3 급 캐 시 가 있 으 며 미리 노출 되 어 방문 과 bean 을 만 드 는 효율 을 크게 향상 시 켰 습 니 다.
6. 코드 블록 보기:
// Guarantee initialization of beans that the current bean depends on.
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dependsOnBean : dependsOn) {
if (isDependent(beanName, dependsOnBean)) {
throw new BeanCreationException("Circular depends-on relationship between '" +
beanName + "' and '" + dependsOnBean + "'");
}
registerDependentBean(dependsOnBean, beanName);
getBean(dependsOnBean);
}
}
설명 은 현재 bean 이 의존 하 는 bean 이 만 들 어 졌 는 지 확인 하 는 것 입 니 다. ioc 의 의존 주입 과 관련 된 것 입 니 다.
7. 코드 블록 보기:
// Create bean instance.
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, new ObjectFactory() {
@Override
public Object getObject() throws BeansException {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
이것 이 야 말로 진정한 bean 인 스 턴 스 를 만 드 는 것 입 니 다.
8. 위의 createBean (beanName, mbd, args) 방법 에 들 어가 서 bean 인 스 턴 스 를 만 듭 니 다.
/**
* Central method of this class: creates a bean instance,
* populates the bean instance, applies post-processors, etc.
* @see #doCreateBean
*/
protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
throws BeanCreationException {
if (logger.isDebugEnabled()) {
logger.debug("Creating instance of bean '" + beanName + "'");
}
// Make sure bean class is actually resolved at this point.
resolveBeanClass(mbd, beanName);
// Prepare method overrides.
try {
mbd.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbd.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbd);
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
}
Object beanInstance = doCreateBean(beanName, mbd, args);
if (logger.isDebugEnabled()) {
logger.debug("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
9, Object beanInstance = doCreateBean (beanName, mbd, args) 에 들 어 갑 니 다:
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
Class> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
// Allow post-processors to modify the merged bean definition.
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
mbd.postProcessed = true;
}
}
// Eagerly cache singletons to be able to resolve circular references
// even when triggered by lifecycle interfaces like BeanFactoryAware.
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isDebugEnabled()) {
logger.debug("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
addSingletonFactory(beanName, new ObjectFactory() {
@Override
public Object getObject() throws BeansException {
return getEarlyBeanReference(beanName, mbd, bean);
}
});
}
// Initialize the bean instance.
Object exposedObject = bean;
try {
populateBean(beanName, mbd, instanceWrapper);
if (exposedObject != null) {
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
}
catch (Throwable ex) {
if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
throw (BeanCreationException) ex;
}
else {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
}
}
if (earlySingletonExposure) {
Object earlySingletonReference = getSingleton(beanName, false);
if (earlySingletonReference != null) {
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set actualDependentBeans = new LinkedHashSet(dependentBeans.length);
for (String dependentBean : dependentBeans) {
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
actualDependentBeans.add(dependentBean);
}
}
if (!actualDependentBeans.isEmpty()) {
throw new BeanCurrentlyInCreationException(beanName,
"Bean with name '" + beanName + "' has been injected into other beans [" +
StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
"] in its raw version as part of a circular reference, but has eventually been " +
"wrapped. This means that said other beans do not use the final version of the " +
"bean. This is often the result of over-eager type matching - consider using " +
"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
}
// Register bean as disposable.
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
return exposedObject;
}
10. instanceWrapper = createBeanInstance (beanName, mbd, args) 에 들 어가 면 createBeanInstance () 방법 은 클래스 를 만 드 는 인 스 턴 스 입 니 다.
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) {
// Make sure bean class is actually resolved at this point.
Class> beanClass = resolveBeanClass(mbd, beanName);
if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
}
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
// Shortcut when re-creating the same bean...
boolean resolved = false;
boolean autowireNecessary = false;
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
if (resolved) {
if (autowireNecessary) {
return autowireConstructor(beanName, mbd, null, null);
}
else {
return instantiateBean(beanName, mbd);
}
}
// Need to determine the constructor...
Constructor>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
return autowireConstructor(beanName, mbd, ctors, args);
}
// No special handling: simply use no-arg constructor.
return instantiateBean(beanName, mbd);
}
마지막 두 줄 코드 를 보면 특별한 처리 가 없고 매개 변수 없 는 구조 방법 을 사용 합 니 다. 클래스 를 만 드 는 인 스 턴 스 는 구조 방법 을 사용 해 야 합 니 다. 여 기 는 기본 적 인 무 참조 구조 방법 으로 bean 인 스 턴 스 를 만 듭 니 다.
11. instantiateBean (beanName, mbd) 에 들 어 갑 니 다.
/**
* Instantiate the given bean using its default constructor.
* @param beanName the name of the bean
* @param mbd the bean definition for the bean
* @return BeanWrapper for the new instance
*/
protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
try {
Object beanInstance;
final BeanFactory parent = this;
if (System.getSecurityManager() != null) {
beanInstance = AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
return getInstantiationStrategy().instantiate(mbd, beanName, parent);
}
}, getAccessControlContext());
}
else {
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
}
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
initBeanWrapper(bw);
return bw;
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
}
}
12. beanInstance = getInstantiationStrategy (). intantiate (mbd, beanName, parent) 에 다시 들 어가 면 bean 의 인 스 턴 스 를 되 돌려 줍 니 다.
public Object instantiate(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) {
// Don't override the class with CGLIB if no overrides.
if (beanDefinition.getMethodOverrides().isEmpty()) {
Constructor> constructorToUse;
synchronized (beanDefinition.constructorArgumentLock) {
constructorToUse = (Constructor>) beanDefinition.resolvedConstructorOrFactoryMethod;
if (constructorToUse == null) {
final Class> clazz = beanDefinition.getBeanClass();
if (clazz.isInterface()) {
throw new BeanInstantiationException(clazz, "Specified class is an interface");
}
try {
if (System.getSecurityManager() != null) {
constructorToUse = AccessController.doPrivileged(new PrivilegedExceptionAction>() {
@Override
public Constructor> run() throws Exception {
return clazz.getDeclaredConstructor((Class[]) null);
}
});
}
else {
constructorToUse = clazz.getDeclaredConstructor((Class[]) null);
}
beanDefinition.resolvedConstructorOrFactoryMethod = constructorToUse;
}
catch (Exception ex) {
throw new BeanInstantiationException(clazz, "No default constructor found", ex);
}
}
}
return BeanUtils.instantiateClass(constructorToUse);
}
else {
// Must generate CGLIB subclass.
return instantiateWithMethodInjection(beanDefinition, beanName, owner);
}
}
여기 서 기본 적 인 무 참 구조 방법 을 해석 해 냈 다.
13. BeanUtils. instantiateClass (constructorToUse) 에 다시 들 어가 서 bean 인 스 턴 스 를 만 듭 니 다.
public static T instantiateClass(Constructor ctor, Object... args) throws BeanInstantiationException {
Assert.notNull(ctor, "Constructor must not be null");
try {
ReflectionUtils.makeAccessible(ctor);
return ctor.newInstance(args);
}
catch (InstantiationException ex) {
throw new BeanInstantiationException(ctor.getDeclaringClass(),
"Is it an abstract class?", ex);
}
catch (IllegalAccessException ex) {
throw new BeanInstantiationException(ctor.getDeclaringClass(),
"Is the constructor accessible?", ex);
}
catch (IllegalArgumentException ex) {
throw new BeanInstantiationException(ctor.getDeclaringClass(),
"Illegal arguments for constructor", ex);
}
catch (InvocationTargetException ex) {
throw new BeanInstantiationException(ctor.getDeclaringClass(),
"Constructor threw exception", ex.getTargetException());
}
}
public static void makeAccessible(Constructor> ctor) {
if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers()))
&& !ctor.isAccessible()) {
ctor.setAccessible(true);
}
}
MakeAccessible () 방법 은 bean 류 구조 방법의 사유 화 를 방지 하여 호출 되 지 못 하 게 하 는 것 입 니 다. 여 기 는 ctor. newInstance (args) 방법 에 중점 을 두 고 있 습 니 다.
14, ctor. newInstance (args) 에 들 어 갑 니 다:
public T newInstance(Object ... initargs)
throws InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException
{
if (!override) {
if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
Class> caller = Reflection.getCallerClass();
checkAccess(caller, clazz, null, modifiers);
}
}
if ((clazz.getModifiers() & Modifier.ENUM) != 0)
throw new IllegalArgumentException("Cannot reflectively create enum objects");
ConstructorAccessor ca = constructorAccessor; // read volatile
if (ca == null) {
ca = acquireConstructorAccessor();
}
@SuppressWarnings("unchecked")
T inst = (T) ca.newInstance(initargs);
return inst;
}
반사 로 bean 인 스 턴 스 를 만 듭 니 다. spring 에서 bean 인 스 턴 스 를 만 드 는 것 은 자바 반사 체 제 를 사용 하여 만 듭 니 다. class 형식 을 가 져 오 면 자바 반사 체 제 를 사용 하여 이러한 인 스 턴 스 를 만 들 고 이 인 스 턴 스 를 되 돌려 줍 니 다. bean 인 스 턴 스 를 만 든 후 인 스 턴 스 를 되 돌려 줍 니 다. spring 은 bean 을 속성 으로 조립 합 니 다. bean 을 spring 캐 시 에 넣 고 beanname 을 key 로 합 니 다. bean 인 스 턴 스 는 value 를 singleton Objects 에 넣 는 동시에 다른 캐 시 를 업데이트 합 니 다. spring 이 두 번 째 로 같은 bean 을 가 져 올 때 캐 시 에서 직접 가 져 오 면 됩 니 다. 새로 만 들 필요 가 없습니다.