首页

通过spring-test源码包BootstrapUtils启动加载器工具类进行初始化加载类bean实例对象源码示例

标签:spring-test,BootstrapUtils,启动加载器工具类,springframework,DefaultBootstrapContext,spring初始化,cacheAwareContextLoaderDelegate,DefaultCacheAwareContextLoaderDelegate     发布时间:2018-09-11   

一、前言

关于spring-test源码包中org.springframework.test.context.BootstrapUtils启动器工具类,通过加载org.springframework.test.context.support.DefaultBootstrapContext获取spring上下文对象,并通过org.springframework.test.context.CacheAwareContextLoaderDelegate加载类到spring容器中,详情参见源码说明部分。

二、源码说明

package org.springframework.test.context;@b@@b@import java.lang.reflect.Constructor;@b@import java.util.Iterator;@b@import java.util.Set;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.springframework.beans.BeanUtils;@b@import org.springframework.core.annotation.AnnotatedElementUtils;@b@import org.springframework.core.annotation.AnnotationAttributes;@b@import org.springframework.util.ClassUtils;@b@@b@abstract class BootstrapUtils@b@{@b@  private static final String DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME = "org.springframework.test.context.support.DefaultBootstrapContext";@b@  private static final String DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME = "org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate";@b@  private static final String DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME = "org.springframework.test.context.support.DefaultTestContextBootstrapper";@b@  private static final String DEFAULT_WEB_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME = "org.springframework.test.context.web.WebTestContextBootstrapper";@b@  private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME = "org.springframework.test.context.web.WebAppConfiguration";@b@  private static final Log logger = LogFactory.getLog(BootstrapUtils.class);@b@@b@  static BootstrapContext createBootstrapContext(Class<?> testClass)@b@  {@b@    CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = createCacheAwareContextLoaderDelegate();@b@    Class clazz = null;@b@    try {@b@      clazz = ClassUtils.forName("org.springframework.test.context.support.DefaultBootstrapContext", BootstrapUtils.class@b@        .getClassLoader());@b@      Constructor constructor = clazz.getConstructor(new Class[] { Class.class, CacheAwareContextLoaderDelegate.class });@b@@b@      if (logger.isDebugEnabled())@b@        logger.debug(String.format("Instantiating BootstrapContext using constructor [%s]", new Object[] { constructor }));@b@@b@      return ((BootstrapContext)BeanUtils.instantiateClass(constructor, new Object[] { testClass, cacheAwareContextLoaderDelegate }));@b@    }@b@    catch (Throwable ex) {@b@      throw new IllegalStateException("Could not load BootstrapContext [" + clazz + "]", ex);@b@    }@b@  }@b@@b@  private static CacheAwareContextLoaderDelegate createCacheAwareContextLoaderDelegate()@b@  {@b@    Class clazz = null;@b@    try {@b@      clazz = ClassUtils.forName("org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate", BootstrapUtils.class@b@        .getClassLoader());@b@@b@      if (logger.isDebugEnabled())@b@        logger.debug(String.format("Instantiating CacheAwareContextLoaderDelegate from class [%s]", new Object[] { clazz@b@          .getName() }));@b@@b@      return ((CacheAwareContextLoaderDelegate)BeanUtils.instantiateClass(clazz, CacheAwareContextLoaderDelegate.class));@b@    }@b@    catch (Throwable ex) {@b@      throw new IllegalStateException("Could not load CacheAwareContextLoaderDelegate [" + clazz + "]", ex);@b@    }@b@  }@b@@b@  static TestContextBootstrapper resolveTestContextBootstrapper(BootstrapContext bootstrapContext)@b@  {@b@    Class testClass = bootstrapContext.getTestClass();@b@@b@    Class clazz = null;@b@    try {@b@      clazz = resolveExplicitTestContextBootstrapper(testClass);@b@      if (clazz == null)@b@        clazz = resolveDefaultTestContextBootstrapper(testClass);@b@@b@      if (logger.isDebugEnabled()) {@b@        logger.debug(String.format("Instantiating TestContextBootstrapper for test class [%s] from class [%s]", new Object[] { testClass@b@          .getName(), clazz.getName() }));@b@      }@b@@b@      TestContextBootstrapper testContextBootstrapper = (TestContextBootstrapper)BeanUtils.instantiateClass(clazz, TestContextBootstrapper.class);@b@@b@      testContextBootstrapper.setBootstrapContext(bootstrapContext);@b@      return testContextBootstrapper;@b@    }@b@    catch (IllegalStateException ex) {@b@      throw ex;@b@    }@b@    catch (Throwable ex) {@b@      throw new IllegalStateException("Could not load TestContextBootstrapper [" + clazz + "]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.", ex);@b@    }@b@  }@b@@b@  private static Class<?> resolveExplicitTestContextBootstrapper(Class<?> testClass)@b@  {@b@    Set annotations = AnnotatedElementUtils.findAllMergedAnnotations(testClass, BootstrapWith.class);@b@    if (annotations.size() < 1)@b@      return null;@b@@b@    if (annotations.size() > 1)@b@      throw new IllegalStateException(String.format("Configuration error: found multiple declarations of @BootstrapWith for test class [%s]: %s", new Object[] { testClass@b@        .getName(), annotations }));@b@@b@    return ((BootstrapWith)annotations.iterator().next()).value();@b@  }@b@@b@  private static Class<?> resolveDefaultTestContextBootstrapper(Class<?> testClass) throws Exception {@b@    ClassLoader classLoader = BootstrapUtils.class.getClassLoader();@b@    AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(testClass, "org.springframework.test.context.web.WebAppConfiguration", false, false);@b@@b@    if (attributes != null)@b@      return ClassUtils.forName("org.springframework.test.context.web.WebTestContextBootstrapper", classLoader);@b@@b@    return ClassUtils.forName("org.springframework.test.context.support.DefaultTestContextBootstrapper", classLoader);@b@  }@b@}