微服务的开始run()

  1. // XXX.class 当前启动类
  2. @EnableSwagger2
  3. @SpringBootApplication
  4. @ComponentScan(basePackages = "com")
  5. @MapperScan(basePackages = {"com.**.dao"})
  6. @EnableFeignClients(basePackages = {"com.**"})
  7. public class StartDemo
  8. {
  9. public static void main( String[] args )
  10. {
  11. SpringApplication.run(StartDemo.class, args);
  12. }
  13. }

调用

  1. public static ConfigurableApplicationContext run(Class<?> primarySource, String... args){
  2. return run(new Class<?>[] { primarySource }, args);
  3. }
  4. public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
  5. return new SpringApplication(primarySources).run(args);
  6. }

public SpringApplication(Class<?>… primarySources) {}

  1. public SpringApplication(Class<?>... primarySources) {
  2. this(null, primarySources);
  3. }
  1. private ResourceLoader resourceLoader;
  2. private Set<Class<?>> primarySources;
  3. private WebApplicationType webApplicationType;
  4. private Class<?> mainApplicationClass;
  5. public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
  6. this.resourceLoader = resourceLoader;
  7. Assert.notNull(primarySources, "PrimarySources must not be null");
  8. this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
  9. this.webApplicationType = WebApplicationType.deduceFromClasspath();
  10. setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
  11. setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
  12. this.mainApplicationClass = deduceMainApplicationClass();
  13. }

setInitializers

  1. public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {
  2. /**
  3. * Initialize the given application context.
  4. * @param applicationContext the application to configure
  5. */
  6. void initialize(C applicationContext);
  7. }
  1. private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
  2. return getSpringFactoriesInstances(type, new Class<?>[] {});
  3. }
  4. private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
  5. ClassLoader classLoader = getClassLoader();
  6. // Use names and ensure unique to protect against duplicates
  7. Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
  8. List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
  9. AnnotationAwareOrderComparator.sort(instances);
  10. return instances;
  11. }
  1. public ClassLoader getClassLoader() {
  2. if (this.resourceLoader != null) {
  3. return this.resourceLoader.getClassLoader();
  4. }
  5. return ClassUtils.getDefaultClassLoader();
  6. }
  7. @Nullable
  8. public static ClassLoader getDefaultClassLoader() {
  9. ClassLoader cl = null;
  10. try {
  11. cl = Thread.currentThread().getContextClassLoader();
  12. }
  13. catch (Throwable ex) {
  14. // Cannot access thread context ClassLoader - falling back...
  15. }
  16. if (cl == null) {
  17. // No thread context class loader -> use class loader of this class.
  18. cl = ClassUtils.class.getClassLoader();
  19. if (cl == null) {
  20. // getClassLoader() returning null indicates the bootstrap ClassLoader
  21. try {
  22. cl = ClassLoader.getSystemClassLoader();
  23. }
  24. catch (Throwable ex) {
  25. // Cannot access system ClassLoader - oh well, maybe the caller can live with null...
  26. }
  27. }
  28. }
  29. return cl;
  30. }
  1. public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
  2. String factoryClassName = factoryClass.getName();
  3. return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
  4. }
  5. private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
  6. MultiValueMap<String, String> result = cache.get(classLoader);
  7. if (result != null) {
  8. return result;
  9. }
  10. try {
  11. Enumeration<URL> urls = (classLoader != null ?
  12. classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
  13. ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
  14. result = new LinkedMultiValueMap<>();
  15. while (urls.hasMoreElements()) {
  16. URL url = urls.nextElement();
  17. UrlResource resource = new UrlResource(url);
  18. Properties properties = PropertiesLoaderUtils.loadProperties(resource);
  19. for (Map.Entry<?, ?> entry : properties.entrySet()) {
  20. String factoryClassName = ((String) entry.getKey()).trim();
  21. for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
  22. result.add(factoryClassName, factoryName.trim());
  23. }
  24. }
  25. }
  26. cache.put(classLoader, result);
  27. return result;
  28. }
  29. catch (IOException ex) {
  30. throw new IllegalArgumentException("Unable to load factories from location [" +
  31. FACTORIES_RESOURCE_LOCATION + "]", ex);
  32. }
  33. }

setListeners

  1. @FunctionalInterface
  2. public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
  3. /**
  4. * Handle an application event.
  5. * @param event the event to respond to
  6. */
  7. void onApplicationEvent(E event);
  8. }

public ConfigurableApplicationContext run(String… args) {}