wordpress自定義登錄地址seo顧問什么職位
1 簡介
1.1 什么是自動裝配?
自動裝配是指 Spring Boot 在啟動時,根據(jù)類路徑上的依賴項自動配置應(yīng)用程序。例如,如果你的應(yīng)用程序依賴于 Spring Data JPA,Spring Boot 會自動配置一個?DataSource
、EntityManagerFactory
?和其他必要的 Bean,而無需你手動編寫這些配置。
1.2 自動裝配的工作原理
-
依賴管理:Spring Boot 使用 Maven 或 Gradle 管理依賴項。當(dāng)你在?
pom.xml
?或?build.gradle
?文件中添加依賴項時,Spring Boot 會檢測這些依賴項。 -
自動配置類:Spring Boot 提供了一組自動配置類,這些類位于?
spring-boot-autoconfigure
?模塊中。每個自動配置類都對應(yīng)一個特定的技術(shù)?;蚬δ苣K。 -
條件注解:自動配置類使用條件注解(如?
@ConditionalOnClass
、@ConditionalOnMissingBean
?等)來決定是否應(yīng)用某個配置。這些注解確保只有在滿足特定條件時才會應(yīng)用配置。
2 源碼解讀
入口:@SpringBootApplication
//以下是@SpringBootApplication注解的內(nèi)部信息 其中最核心的是后3條
@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) })
2.1 @SpringBootConfiguration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
//這個注解的主要主要表明這是一個配置類 需要解析
public @interface SpringBootConfiguration {/*** Specify whether {@link Bean @Bean} methods should get proxied in order to enforce* bean lifecycle behavior, e.g. to return shared singleton bean instances even in* case of direct {@code @Bean} method calls in user code. This feature requires* method interception, implemented through a runtime-generated CGLIB subclass which* comes with limitations such as the configuration class and its methods not being* allowed to declare {@code final}.* <p>* The default is {@code true}, allowing for 'inter-bean references' within the* configuration class as well as for external calls to this configuration's* {@code @Bean} methods, e.g. from another configuration class. If this is not needed* since each of this particular configuration's {@code @Bean} methods is* self-contained and designed as a plain factory method for container use, switch* this flag to {@code false} in order to avoid CGLIB subclass processing.* <p>* Turning off bean method interception effectively processes {@code @Bean} methods* individually like when declared on non-{@code @Configuration} classes, a.k.a.* "@Bean Lite Mode" (see {@link Bean @Bean's javadoc}). It is therefore behaviorally* equivalent to removing the {@code @Configuration} stereotype.* @return whether to proxy {@code @Bean} methods* @since 2.2*/@AliasFor(annotation = Configuration.class)boolean proxyBeanMethods() default true;}
2.2 @EnableAutoConfiguration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
//自動注冊一個basepackage的bean 作用是存儲掃描路徑 給其他組件使用
@AutoConfigurationPackage
//實(shí)現(xiàn)自動裝配
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {/*** Environment property that can be used to override when auto-configuration is* enabled.*/String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";/*** Exclude specific auto-configuration classes such that they will never be applied.* @return the classes to exclude*/Class<?>[] exclude() default {};/*** Exclude specific auto-configuration class names such that they will never be* applied.* @return the class names to exclude* @since 1.3.0*/String[] excludeName() default {};}
2.2.1 @AutoConfigurationPackage
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
//引入注冊器注冊basepackage的bean
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {/*** Base packages that should be registered with {@link AutoConfigurationPackages}.* <p>* Use {@link #basePackageClasses} for a type-safe alternative to String-based package* names.* @return the back package names* @since 2.3.0*/String[] basePackages() default {};/*** Type-safe alternative to {@link #basePackages} for specifying the packages to be* registered with {@link AutoConfigurationPackages}.* <p>* Consider creating a special no-op marker class or interface in each package that* serves no purpose other than being referenced by this attribute.* @return the base package classes* @since 2.3.0*/Class<?>[] basePackageClasses() default {};}static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {@Overridepublic void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {//注冊basepackage的beanregister(registry, new PackageImports(metadata).getPackageNames().toArray(new String[0]));}@Overridepublic Set<Object> determineImports(AnnotationMetadata metadata) {return Collections.singleton(new PackageImports(metadata));}}
2.2.2 @Import(AutoConfigurationImportSelector.class)
//這個我們先看實(shí)現(xiàn)的接口
//1 實(shí)現(xiàn)了DeferredImportSelector 而DeferredImportSelector繼承了ImportSelector 作用是用來注冊bean
//2 DeferredImportSelector對ImportSelector進(jìn)行了增強(qiáng) 只有其他配置類掃描完了后 再解析這個引入的bean 為了確保條件注解的正確性
//3 重寫了DeferredImportSelector的getImportGroup方法 調(diào)用好 優(yōu)先會調(diào)用getImportGroup方法
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware,ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {}public Class<? extends Group> getImportGroup() {//spring為注冊分組的類 會自動調(diào)用該類的process方法return AutoConfigurationGroup.class;
} public void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) {Assert.state(deferredImportSelector instanceof AutoConfigurationImportSelector,() -> String.format("Only %s implementations are supported, got %s",AutoConfigurationImportSelector.class.getSimpleName(),deferredImportSelector.getClass().getName()));//獲取自動配置類 AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector).getAutoConfigurationEntry(annotationMetadata);this.autoConfigurationEntries.add(autoConfigurationEntry);for (String importClassName : autoConfigurationEntry.getConfigurations()) {this.entries.putIfAbsent(importClassName, annotationMetadata);}
}protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return EMPTY_ENTRY;}//1 獲取注解的屬性AnnotationAttributes attributes = getAttributes(annotationMetadata);//2 掃描自動裝配類List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);//3 去重configurations = removeDuplicates(configurations);//4 獲取過濾的bean集Set<String> exclusions = getExclusions(annotationMetadata, attributes);checkExcludedClasses(configurations, exclusions);//5 移除過濾的beanconfigurations.removeAll(exclusions);configurations = getConfigurationClassFilter().filter(configurations);fireAutoConfigurationImportEvents(configurations, exclusions);return new AutoConfigurationEntry(configurations, exclusions);
}protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {//加載配置文件獲取配置List<String> configurations = new ArrayList<>(SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()));ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);Assert.notEmpty(configurations,"No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you "+ "are using a custom packaging, make sure that file is correct.");return configurations;
}public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {ClassLoader classLoaderToUse = classLoader;if (classLoader == null) {classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();}String factoryTypeName = factoryType.getName();//加載配置文件獲取配置return (List)loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
}private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {Map<String, List<String>> result = (Map)cache.get(classLoader);if (result != null) {return result;} else {Map<String, List<String>> result = new HashMap();try {//從META-INF/spring.factories獲取所有的自動裝配類Enumeration<URL> urls = classLoader.getResources("META-INF/spring.factories");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()) {Map.Entry<?, ?> entry = (Map.Entry)var6.next();String factoryTypeName = ((String)entry.getKey()).trim();String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());String[] var10 = factoryImplementationNames;int var11 = factoryImplementationNames.length;for(int var12 = 0; var12 < var11; ++var12) {String factoryImplementationName = var10[var12];((List)result.computeIfAbsent(factoryTypeName, (key) -> {return new ArrayList();})).add(factoryImplementationName.trim());}}}result.replaceAll((factoryType, implementations) -> {return (List)implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));});cache.put(classLoader, result);return result;} catch (IOException var14) {throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var14);}}
}
2.3 @ComponentScan()
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware {private BeanFactory beanFactory;private Collection<TypeExcludeFilter> delegates;@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {this.beanFactory = beanFactory;}@Overridepublic boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)throws IOException {//去容器中獲取所有類型為TypeExcludeFilter的bean 去執(zhí)行過濾//我們可以實(shí)現(xiàn)這個方法 重寫match方法 完成過濾if (this.beanFactory instanceof ListableBeanFactory && getClass() == TypeExcludeFilter.class) {for (TypeExcludeFilter delegate : getDelegates()) {if (delegate.match(metadataReader, metadataReaderFactory)) {return true;}}}return false;}private Collection<TypeExcludeFilter> getDelegates() {Collection<TypeExcludeFilter> delegates = this.delegates;if (delegates == null) {delegates = ((ListableBeanFactory) this.beanFactory).getBeansOfType(TypeExcludeFilter.class).values();this.delegates = delegates;}return delegates;}@Overridepublic boolean equals(Object obj) {throw new IllegalStateException("TypeExcludeFilter " + getClass() + " has not implemented equals");}@Overridepublic int hashCode() {throw new IllegalStateException("TypeExcludeFilter " + getClass() + " has not implemented hashCode");}}
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) public class AutoConfigurationExcludeFilter implements TypeFilter, BeanClassLoaderAware {private ClassLoader beanClassLoader;private volatile List<String> autoConfigurations;@Overridepublic void setBeanClassLoader(ClassLoader beanClassLoader) {this.beanClassLoader = beanClassLoader;}@Overridepublic boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)throws IOException {//1 是否是一個配置類//2 是否有AutoConfiguration或者在自動裝配類已配置//同時滿足上面兩個條件就不掃描這個beanreturn isConfiguration(metadataReader) && isAutoConfiguration(metadataReader);}private boolean isConfiguration(MetadataReader metadataReader) {return metadataReader.getAnnotationMetadata().isAnnotated(Configuration.class.getName());}private boolean isAutoConfiguration(MetadataReader metadataReader) {boolean annotatedWithAutoConfiguration = metadataReader.getAnnotationMetadata().isAnnotated(AutoConfiguration.class.getName());return annotatedWithAutoConfiguration|| getAutoConfigurations().contains(metadataReader.getClassMetadata().getClassName());}protected List<String> getAutoConfigurations() {if (this.autoConfigurations == null) {List<String> autoConfigurations = new ArrayList<>(SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, this.beanClassLoader));ImportCandidates.load(AutoConfiguration.class, this.beanClassLoader).forEach(autoConfigurations::add);this.autoConfigurations = autoConfigurations;}return this.autoConfigurations;}}