当前位置: 首页 > >

SpringBootÔ´Âë½âÎö-@SpringBootApplication×¢½â

发布时间:

¶ÔÒ»¸öspring bootÏîÄ¿£¬ÆäÆô¶¯ÀàÉÏ»á¼ÓÉÏÕâ¸ö×¢½â£¬ÄÇÕâ¸ö×¢½âÊǸÉÂïµÄÄØ£¿


1.@SpringBootApplication

¿´Ò»ÏÂ×¢½âµÄ¶¨Òå


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication

@ComponentScanÊǶ¨ÒåɨÃè°ü£¬@SpringBootConfiguration¶¨ÒåÒ»¸öÅäÖÃÀ࣬µÈ¼ÛÓÚ@Configuration£¬È»ºó¾ÍÊÇ@EnableAutoConfiguration×¢½â


2.@EnableAutoConfiguration

@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 {};
}

ÕâÀï¼ÓÁË@AutoConfigurationPackageºÍ@Import(AutoConfigurationImportSelector.class)Á½¸ö×¢½â

ÆäÖÐ@AutoConfigurationPackage×¢½âµÄ×÷ÓÃÊǽ« Ìí¼Ó¸Ã×¢½âµÄÀàËùÔÚµÄpackage ×÷Ϊ ×Ô¶¯ÅäÖÃpackage ½øÐйÜÀí


@ImportÊÇÏòÈÝÆ÷ÖÐÌí¼Ó¶ÔÓ¦×é¼þ


3.AutoConfigurationImportSelector

Ö±½Ó·­Òë¾ÍÊÇ×Ô¶¯ÅäÖõ¼ÈëÑ¡ÔñÆ÷£¬¾ÍÊǽøÐÐ×é¼þ×°ÅäµÄ£¬ÎÒÃÇ¿´Ò»ÏÂÀඨÒå


public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware,
ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {

¸ÃÀàʵÏÖÁËDeferredImportSelector½Ó¿Ú


public interface DeferredImportSelector extends ImportSelector

DeferredImportSelector½Ó¿Ú¼Ì³Ð×ÔImportSelector½Ó¿Ú£¬ËùÒÔÆäselectImports·½·¨»á±»ÊµÏÖ£¬ÎÒÃÇ¿´Ò»ÏÂÆä¾ßÌåÄÚÈÝ


@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}

Ê×ÏÈÊÇУÑ飬Ȼºó»ñÈ¡AutoConfigurationEntry×îºóת»»³É×Ö·û´®Êý×飬ÕâÀïÖصã¾ÍÊÇÈçºÎ»ñÈ¡AutoConfigurationEntryÏÂÃæ¿´Ò»ÏÂgetAutoConfigurationEntry·½·¨µÄʵÏÖ


protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
//»ñȡע½âÀïÉèÖõÄÊôÐÔ£¬ÔÚ@SpringBootApplicationÉèÖõÄexclude,excludeNameÊôÐÔÖµ£¬
//Æäʵ¾ÍÊÇÉèÖÃ@EnableAutoConfigurationµÄÕâÁ½¸öÊôÐÔÖµ
AnnotationAttributes attributes = getAttributes(annotationMetadata);
//»ñÈ¡ËùÓÐjar°üÀïÃæMETA-INF/spring.factories¼ÓÔØÅäÖÃÀàµÄÃû³Æ£¬
// ´ò¿ªÕâ¸öÎļþ·¢ÏÖÀïÃæ°üº¬ÁËspringboot¿ò¼ÜÌṩµÄËùÓÐÅäÖÃÀà
List configurations = getCandidateConfigurations(annotationMetadata, attributes);
//È¥ÖزÙ×÷
configurations = removeDuplicates(configurations);
//»ñÈ¡×Ô¼ºÅäÖò»ÐèÒªÉú³ÉbeanµÄclass
Set exclusions = getExclusions(annotationMetadata, attributes);
//УÑé±»excludeµÄÀàÊÇ·ñ¶¼ÊÇspringboot×Ô¶¯»¯ÅäÖÃÀïµÄÀ࣬Èç¹û´æÔÚÅ׳öÒì³£
checkExcludedClasses(configurations, exclusions);
//ɾ³ý±»excludeµôµÄÀà
configurations.removeAll(exclusions);
//¹ýÂËˢѡ£¬Âú×ãOnClassConditionµÄÀà
configurations = getConfigurationClassFilter().filter(configurations);
fireAutoConfigurationImportEvents(configurations, exclusions);
//·â×°·µ»Ø
return new AutoConfigurationEntry(configurations, exclusions);
}

Öصã¾ÍÊÇgetCandidateConfigurations(annotationMetadata, attributes)·½·¨ÁË£¬ÎÒÃÇÀ´¿´Ò»Ï°É


protected List getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
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;
}

protected Class getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}

ÕâÀïµ÷ÓÃÁËSpringFactoriesLoader.loadFactoryNames·½·¨£¬ÎÒÃǼÌÐø¿´°É


/**
* ¼ÓÔØ×ÊÔ´
*/
public static List loadFactoryNames(Class factoryType, @Nullable ClassLoader classLoader) {
//»ñÈ¡factoryTypeµÄÈ«ÀàÃû
String factoryTypeName = factoryType.getName();

//¼ÓÔØSpringFactories²¢»ñÈ¡factoryTypeName¶ÔÓ¦×ÊÔ´
//getOrDefault±£Ö¤Èôδ»ñÈ¡µ½¶ÔÓ¦×ÊÔ´Ôò·µ»ØÒ»¸ö¿Õ¼¯ºÏ
return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}

È»ºó¾ÍÊÇloadSpringFactories·½·¨


/**
* ¼ÓÔØSpringFactories£¬Ò²¾ÍÊÇÒª¼ÓÔØMETA-INF/spring.factoriesÎļþÖеÄÄÚÈÝ
* @param classLoader Àà¼ÓÔ*÷
*/
private static Map> loadSpringFactories(@Nullable ClassLoader classLoader) {
//Ïȼì²écache»º´æ£¬±ÜÃâÖظ´¼ÓÔØ
MultiValueMap result = cache.get(classLoader);
if (result != null) {
return result;
}

try {
//»ñÈ¡ËùÓеÄMETA-INF/spring.factoriesÎļþ·¾¶
Enumeration urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));

//³õʼ»¯resultΪһ¸öLinkedMultiValueMap
result = new LinkedMultiValueMap<>();

//±éÀúËùÓÐurls ¹ØÓÚEnumeration½Ó¿ÚʵÏֿɲο¼CompoundEnumeration
while (urls.hasMoreElements()) {
//»ñÈ¡Ò»¸öurl
URL url = urls.nextElement();
//½«url°ü×°³ÉÒ»¸öUrlResource
UrlResource resource = new UrlResource(url);
//¼ÓÔØUrlResource²¢×ª»»³ÉProperties
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
//±éÀúpropertiesµÄËùÓнڵã
for (Map.Entry entry : properties.entrySet()) {
//»ñÈ¡key
String factoryTypeName = ((String) entry.getKey()).trim();
//±éÀú½á¹û¼¯£¬Ìí¼Óµ½resultÖÐ
for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
result.add(factoryTypeName, factoryImplementationName.trim());
}
}
}
//¼ÓÈ뻺´æÖÐ
cache.put(classLoader, result);
return result; //·µ»Ø½á¹û
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
}

FACTORIES_RESOURCE_LOCATION·¾¶¶¨Òå


public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";

µ½´Ë½áÊø



友情链接: 简历 面试求职范文 职业规划 自我管理 社交礼仪 76242百科