1. Parent project:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.12.RELEASE</version> </parent>
Its parent project is:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
The parent project is the version that actually manages all dependencies in the Spring Boot application: the version arbitration center of Spring Boot, so the dependencies imported later do not need a version number by default.
2. Launcher
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
: The spring boot scene starter; helps import the components that the web module depends on for normal operation;
Spring Boot extracts all functional scenarios and makes them into starters (starters) one by one. You only need to introduce these starters into the project, then all dependencies of related scenarios will be imported into the project. A launcher for importing what kind of scene you want to use.
3. Main program class (main entry class)
/** * @SpringBootApplication to mark a main program class, indicating that this is a SpringBoot application */ @SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { //Spring application start SpringApplication.run(HelloWorldMainApplication.class, args); } }
3.1、@SpringBootApplication
Annotations are defined as follows:
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {}
3.2、@SpringBootConfiguration:
Annotated on a class, indicating that this is a Spring Boot configuration class
Annotations are defined as follows:
@Configuration public @interface SpringBootConfiguration {}
3.3、@Configuration:
Annotate this annotation, corresponding to the configuration file in the Spring application, and its bottom layer is a component @Component in the container . Annotations are defined as follows:
@Component public @interface Configuration {}
3.4、@EnableAutoConfiguration:
The information that needs to be configured before using Spring, Spring Boot helps with automatic configuration;
@EnableAutoConfiguration informs SpringBoot to turn on the automatic configuration function, so that the automatic configuration can take effect. Annotations are defined as follows:
@AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {}
3.5、@AutoConfigurationPackage:
@Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage {}
3.6、@Import:
Import components into the container, the components are specified by the value of @Import ;
@Order(Ordered.HIGHEST_PRECEDENCE) static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports { @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { //By default, all components under the package where the main configuration class marked by @SpringBootApplication is located and its subpackages will be scanned register(registry, new PackageImport(metadata).getPackageName()); } @Override public Set<Object> determineImports(AnnotationMetadata metadata) { return Collections.<Object>singleton(new PackageImport(metadata)); } }
: Import components into the container
EnableAutoConfigurationImportSelector : Selector of which components to import, return all components that need to be imported as full class names, and these components will be added to the container.
//EnableAutoConfigurationImportSelector的父类:AutoConfigurationImportSelector @Override public String[] selectImports(AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return NO_IMPORTS; } try { AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader .loadMetadata(this.beanClassLoader); AnnotationAttributes attributes = getAttributes(annotationMetadata); List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); configurations = removeDuplicates(configurations); configurations = sort(configurations, autoConfigurationMetadata); Set<String> exclusions = getExclusions(annotationMetadata, attributes); checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = filter(configurations, autoConfigurationMetadata); fireAutoConfigurationImportEvents(configurations, exclusions); return configurations.toArray(new String[configurations.size()]); } catch (IOException ex) { throw new IllegalStateException(ex); } }, many automatic configuration classes (xxxAutoConfiguration) will be injected into the container, which is to import all the components required for this scene into the container and configure these components.
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
With automatic configuration classes, the work of manually writing configuration classes to inject functional components is eliminated.
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); //... return configurations; }
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"; public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) { String factoryClassName = factoryClass.getName(); try { //Load all default auto-configuration classes from META-INF/spring.factories on the classpath Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); List<String> result = new ArrayList<String>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); //Get all the values specified by EnableAutoConfiguration String factoryClassNames = properties.getProperty(factoryClassName); result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames))); } return result; } catch (IOException ex) { throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() + "] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); } }
The overall integration solution and automatic configuration of J2EE are in spring-boot-autoconfigure-xxx.jar.
=====================Make an advertisement, welcome to pay attention =====================
QQ: |
412425870 |
WeChat public account: Cay Classroom |
|
csdn blog: |
http://blog.csdn.net/caychen |
Code cloud: |
https://gitee.com/caychen/ |
github: |
https://github.com/caychen |
Click on the group number or scan the QR code to join the QQ group: |
|
Click on the group number or scan the QR code to join the QQ group: |