import org.springframework.boot.SpringApplication; import
org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello
world! * */ @SpringBootApplication public class App { public static void main(
String[] args ) { System.out.println( "Hello World!" );
SpringApplication.run(App.class,args); } }
one ,Spring SPI mechanism , automatic assembly :
Startup class usage @SpringApplication annotation , Look at the annotation code :
Used in code @EnableAutoConfiguration as well as @ComponentScan automatic assembly
Where notes @EnableAutoConfiguration Used @Import load , Finally used SpringFactoriesLoader Reflex out maven in META-INF lower spring.factories.
public @interface SpringBootApplication { @AliasFor( annotation =
EnableAutoConfiguration.class ) Class<?>[] exclude() default {}; @AliasFor(
annotation = EnableAutoConfiguration.class ) String[] excludeName() default {};
@AliasFor( annotation = ComponentScan.class, attribute = "basePackages" )
String[] scanBasePackages() default {}; @AliasFor( annotation =
ComponentScan.class, attribute = "basePackageClasses" ) Class<?>[]
scanBasePackageClasses() default {}; } package
org.springframework.boot.autoconfigure; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType; import java.lang.annotation.Inherited;
import java.lang.annotation.Retention; import
java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented
@Inherited @AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class}) public @interface
EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY =
"spring.boot.enableautoconfiguration"; Class<?>[] exclude() default {};
String[] excludeName() default {}; } // public class
AutoConfigurationImportSelector ...{ ... protected List<String>
getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes
attributes) { // Under the loading classpath META-INF/spring.factories List<String> configurations =
SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(),
this.getBeanClassLoader()); Assert.notEmpty(configurations, "No auto
configuration classes found in META-INF/spring.factories. If you are using a
custom packaging, make sure that file is correct."); return configurations; } }
public abstract class SpringFactoriesLoader { public static final String
FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"; ..... public static
List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader
classLoader) { String factoryClassName = factoryClass.getName(); return
(List)loadSpringFactories(classLoader).getOrDefault(factoryClassName,
Collections.emptyList()); } private static Map<String, List<String>>
loadSpringFactories(@Nullable ClassLoader classLoader) { MultiValueMap<String,
String> result = (MultiValueMap)cache.get(classLoader); if (result != null) {
return result; } else { try { Enumeration<URL> urls = classLoader != null ?
classLoader.getResources("META-INF/spring.factories") :
ClassLoader.getSystemResources("META-INF/spring.factories");
LinkedMultiValueMap result = new LinkedMultiValueMap();
while(urls.hasMoreElements()) { URL url = (URL)urls.nextElement(); UrlResource
resource = new UrlResource(url); Properties properties =
PropertiesLoaderUtils.loadProperties(resource); Iterator var6 =
properties.entrySet().iterator(); while(var6.hasNext()) { Entry<? ?> entry =
(Entry)var6.next(); List<String> factoryClassNames =
Arrays.asList(StringUtils.commaDelimitedListToStringArray((String)entry.getValue()));
result.addAll((String)entry.getKey(), factoryClassNames); } }
cache.put(classLoader, result); return result; } catch (IOException var9) {
throw new IllegalArgumentException("Unable to load factories from location
[META-INF/spring.factories]", var9); } } } .... }
Interested Lao tie , You can have a look java of SPI and Spring of SPI, Automatic assembly mechanism
two ,SpringBoot Start by executing main In method SpringApplication.run Method to start , stay run Method called SpringApplication Construction method of , Loaded in this constructor META-INFA\spring.factories File configured ApplicationContextInitializer Implementation classes and ApplicationListenerr Implementation class of
:
SpringApplication.run(....)
two ,ApplicationContextInitializer This class when springboot context Context Called after initialization .
ApplicationListener When springboot Event on startup change Will be triggered after .
three ,SpringApplication The instance will be called after it is constructed run method , stay run The following important steps are made in the method :
1. Get event listener SpringApplicationRunListener type , And execute starting() method
2. Prepare the environment , And compare the environment with spring Good context binding , And execute environmentPrepared() method
3. Create context , Create context based on project type
4. implement spring Start process scan and initialize single real column bean
four , adopt @SpringBootApplication Annotation will ClassPath All under the path META-INF\spring.factories In the file EnableAutoConfiguration Instance injection into IOC In container
Technology