当前位置: 首页 > news >正文

网站营销费用网络软文

网站营销费用,网络软文,ppt模板免费下载 素材熊猫办公,网站建设相关专业概述 Spring Boot自动装配是其核心特性之一,它能够根据项目中添加的依赖自动配置Spring应用程序。通过自动装配,开发人员可以减少大量的配置工作,快速搭建起一个可用的Spring应用。 关键组件和注解 SpringBootApplication注解 这是Spring Bo…
  1. 概述

    • Spring Boot自动装配是其核心特性之一,它能够根据项目中添加的依赖自动配置Spring应用程序。通过自动装配,开发人员可以减少大量的配置工作,快速搭建起一个可用的Spring应用。
  2. 关键组件和注解

    • @SpringBootApplication注解
      • 这是Spring Boot应用的主注解,它是一个组合注解,实际上包含了@Configuration@EnableAutoConfiguration@ComponentScan三个注解。
      • @Configuration:表明这个类是一个配置类,用于定义Spring的Bean。在配置类中,可以通过@Bean注解方法来创建和配置Bean实例。例如:
        @Configuration
        public class AppConfig {@Beanpublic MyService myService() {return new MyServiceImpl();}
        }
        
        • 这里定义了一个名为myService的Bean,类型是MyService,其实现是MyServiceImpl
      • @EnableAutoConfiguration:这是自动装配的关键注解。它会启用Spring Boot的自动配置机制,告诉Spring Boot去根据项目的依赖和配置自动配置应用。它通过@Import注解导入了AutoConfigurationImportSelector类来实现自动配置的功能。
      • @ComponentScan:用于扫描指定包及其子包下的组件(如@Component@Service@Repository@Controller等注解标记的类),将它们注册为Spring的Bean。默认情况下,它会扫描主应用类所在的包及其子包。例如,如果主应用类在com.example.myapp包下,那么@ComponentScan会扫描com.example.myapp及其所有子包下的组件。
    • AutoConfigurationImportSelector
      • 这个类是自动装配的核心实现类。它实现了ImportSelector接口,该接口的selectImports方法用于返回要导入的配置类的全限定名数组。
      • AutoConfigurationImportSelector中,selectImports方法会从META - INF/spring.factories文件中读取自动配置类的列表。它通过SpringFactoriesLoader.loadFactoryNames方法来加载这些配置类,例如:
        public String[] selectImports(AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return NO_IMPORTS;}AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
        }
        
      • 这里的getAutoConfigurationEntry方法会获取需要自动导入的配置类,这些配置类是根据项目中的依赖和条件来确定的。
  3. META - INF/spring.factories文件机制

    • 这是自动装配的重要配置文件。在Spring Boot的各个依赖中,都可以包含META - INF/spring.factories文件。
    • 这个文件的格式是key = value的形式,其中一个关键的配置项是org.springframework.boot.autoconfigure.EnableAutoConfiguration。它的值是一个自动配置类的列表,例如:
      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\// 其他自动配置类
      
    • 当Spring Boot启动时,AutoConfigurationImportSelector会读取这些文件,找到所有的自动配置类。然后,根据条件注解(如@ConditionalOnClass@ConditionalOnMissingBean等)来判断这些自动配置类是否应该被应用。
    • 条件注解示例
      • @ConditionalOnClass:这个注解用于判断某个类是否在类路径上。例如,DataSourceAutoConfiguration中有如下注解:
        @Configuration
        @ConditionalOnClass({DataSource.class, EmbeddedDatabaseType.class})
        public class DataSourceAutoConfiguration {// 配置内容
        }
        
        • 这表示只有当DataSource类和EmbeddedDatabaseType类都在类路径上时,DataSourceAutoConfiguration这个自动配置类才会被应用。这样可以确保只有在项目中添加了相关的数据源依赖时,才会进行数据源的自动配置。
      • @ConditionalOnMissingBean:用于判断某个类型的Bean是否不存在。例如,在某个自动配置类中定义一个方法来配置一个Bean:
        @Bean
        @ConditionalOnMissingBean
        public MyBean myBean() {return new MyBeanImpl();
        }
        
        • 这表示只有当容器中不存在MyBean类型的Bean时,才会创建并添加MyBeanImpl这个Bean到容器中。
  4. 自动配置的过程

    • Spring Boot应用启动时,首先会加载主应用类,由于@SpringBootApplication注解的存在,@Configuration注解使得这个类被视为一个配置类,@ComponentScan开始扫描组件。
    • 同时,@EnableAutoConfiguration触发自动装配过程。AutoConfigurationImportSelector从各个依赖的META - INF/spring.factories文件中获取自动配置类列表。
    • 然后,对于每个自动配置类,根据条件注解来检查是否满足应用条件。如果满足条件,就会将这个自动配置类加载到Spring容器中,自动配置类中的@Bean方法会被调用,创建和配置相应的Bean,从而完成自动装配的过程。

具体源码讲解

  1. @SpringBootApplication注解源码分析

    • 首先查看@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 = AutoExcludeFilter.class)
    })
    public class SpringBootApplication {
    }
    
    • 可以看到它是一个组合注解,包含了@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan
    • @SpringBootConfiguration:它实际上就是@Configuration注解,用于标识这个类是一个配置类。
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Configuration
    public class SpringBootConfiguration {
    }
    
    • @EnableAutoConfiguration:这是自动装配的关键。它的源码如下:
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage
    @Import(AutoConfigurationImportSelector.class)
    public class EnableAutoConfiguration {
    }
    
    • 其中@Import(AutoConfigurationImportSelector.class)是核心部分。这个AutoConfigurationImportSelector类用于加载自动配置类。
    • @ComponentScan:用于扫描组件,它有一些默认的扫描规则和可以自定义的过滤规则,如上述代码中的excludeFilters,用于排除某些类型的组件扫描。
  2. AutoConfigurationImportSelector源码分析

    • AutoConfigurationImportSelector实现了ImportSelector接口,其中关键的方法是selectImports
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return NO_IMPORTS;}AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
    }
    
    • 这个方法首先检查自动配置是否启用。然后通过AutoConfigurationMetadataLoader.loadMetadata加载自动配置元数据,再通过getAutoConfigurationEntry获取自动配置项。
    • 深入getAutoConfigurationEntry方法:
    protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata,AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return EMPTY_ENTRY;}AnnotationAttributes attributes = getAttributes(annotationMetadata);List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);configurations = removeDuplicates(configurations);Set<String> exclusions = getExclusions(annotationMetadata, attributes);checkExcludedClasses(configurations, exclusions);configurations.removeAll(exclusions);configurations = filter(configurations, autoConfigurationMetadata);fireAutoConfigurationImportEvents(configurations, exclusions);return new AutoConfigurationEntry(configurations, exclusions);
    }
    
    • 在这里,getCandidateConfigurations方法用于获取候选的自动配置类列表,它会从META - INF/spring.factories文件中读取相关配置:
    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {List<String> 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;
    }
    
    • SpringFactoriesLoader.loadFactoryNames方法会读取META - INF/spring.factories文件,查找org.springframework.boot.autoconfigure.EnableAutoConfiguration对应的配置类列表。
  3. META - INF/spring.factories文件读取源码分析

    • SpringFactoriesLoader类用于读取spring.factories文件,关键方法是loadFactoryNames
    public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {String factoryTypeName = factoryType.getName();return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
    }
    
    • 它调用了loadSpringFactories方法:
    private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {MultiValueMap<String, String> result = cache.get(classLoader);if (result!= null) {return result;}try {Enumeration<URL> urls = (classLoader!= null?classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));result = new LinkedMultiValueMap<>();while (urls.hasMoreElements()) {URL url = urls.nextElement();UrlResource resource = new UrlResource(url);Properties properties = PropertiesLoaderUtils.loadProperties(resource);for (Map.Entry<Object, Object> entry : properties.entrySet()) {String factoryTypeName = ((String) entry.getKey()).trim();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);}
    }
    
    • 这个方法首先检查缓存中是否已经有读取的结果。如果没有,就通过ClassLoader获取META - INF/spring.factories文件的URL,然后将其内容读取到Properties对象中。最后,将文件中的配置解析出来,以key - value的形式存储在MultiValueMap中,其中key是配置项的类型(如org.springframework.boot.autoconfigure.EnableAutoConfiguration),value是对应的配置类列表。
  4. 条件注解的源码体现(以@ConditionalOnClass为例)

    • @ConditionalOnClass注解用于判断某个类是否在类路径上。它的实现基于Condition接口。
    • 当Spring容器在处理自动配置类时,会检查条件注解。@ConditionalOnClass对应的Condition实现类是OnClassCondition。在自动配置类加载过程中,会调用OnClassConditionmatches方法来判断条件是否满足:
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {// 省略部分代码List<String> onClasses = getCandidates(metadata, ConditionalOnClass.class);if (onClasses!= null) {List<String> missing = filter(onClasses, ClassNameFilter.MISSING, context);if (!missing.isEmpty()) {return false;}}List<String> onMissingClasses = getCandidates(metadata, ConditionalOnMissingClass.class);if (onMissingClasses!= null) {List<String> present = filter(onMissingClasses, ClassNameFilter.PRESENT, context);if (!present.isEmpty()) {return false;}}return true;
    }
    
    • 这个方法会获取@ConditionalOnClass注解中指定的类列表,然后检查这些类是否在类路径上。如果有任何一个指定的类不存在,就返回false,表示条件不满足,自动配置类不会被加载。通过这样的机制,Spring Boot可以根据类的存在与否来决定自动配置类的加载与否,实现智能的自动装配。

文章转载自:
http://clocking.bnpn.cn
http://emendatory.bnpn.cn
http://zygosperm.bnpn.cn
http://electroconvulsive.bnpn.cn
http://tithing.bnpn.cn
http://endomorphic.bnpn.cn
http://eurycephalic.bnpn.cn
http://seafood.bnpn.cn
http://wimpish.bnpn.cn
http://amenophis.bnpn.cn
http://hermaphrodite.bnpn.cn
http://campy.bnpn.cn
http://scriber.bnpn.cn
http://anime.bnpn.cn
http://vittle.bnpn.cn
http://earing.bnpn.cn
http://avocet.bnpn.cn
http://personalist.bnpn.cn
http://chiral.bnpn.cn
http://escapeproof.bnpn.cn
http://turbotrain.bnpn.cn
http://aspheric.bnpn.cn
http://amply.bnpn.cn
http://quashy.bnpn.cn
http://teleseme.bnpn.cn
http://recitable.bnpn.cn
http://leadwort.bnpn.cn
http://shikker.bnpn.cn
http://adipsia.bnpn.cn
http://euhemerus.bnpn.cn
http://randem.bnpn.cn
http://tumbledown.bnpn.cn
http://bmr.bnpn.cn
http://leatherworking.bnpn.cn
http://farside.bnpn.cn
http://oldness.bnpn.cn
http://flood.bnpn.cn
http://potecary.bnpn.cn
http://huskiness.bnpn.cn
http://autoeroticism.bnpn.cn
http://mellifluent.bnpn.cn
http://prepositional.bnpn.cn
http://padre.bnpn.cn
http://analeptic.bnpn.cn
http://overleaf.bnpn.cn
http://pungi.bnpn.cn
http://arthrotomy.bnpn.cn
http://skiagram.bnpn.cn
http://sdcd.bnpn.cn
http://xylographer.bnpn.cn
http://placeholder.bnpn.cn
http://imprudent.bnpn.cn
http://quartertone.bnpn.cn
http://choragus.bnpn.cn
http://artsy.bnpn.cn
http://dts.bnpn.cn
http://bromidic.bnpn.cn
http://contradict.bnpn.cn
http://actinometer.bnpn.cn
http://tisiphone.bnpn.cn
http://pyrogenic.bnpn.cn
http://diaphoneme.bnpn.cn
http://gallice.bnpn.cn
http://molluscicide.bnpn.cn
http://poleaxe.bnpn.cn
http://tripitaka.bnpn.cn
http://antinatalist.bnpn.cn
http://unfishable.bnpn.cn
http://stereoscopic.bnpn.cn
http://truthfully.bnpn.cn
http://oscillation.bnpn.cn
http://dunny.bnpn.cn
http://prosthetics.bnpn.cn
http://summerset.bnpn.cn
http://gopi.bnpn.cn
http://crucian.bnpn.cn
http://overintricate.bnpn.cn
http://montmorillonite.bnpn.cn
http://nourish.bnpn.cn
http://intuitively.bnpn.cn
http://massif.bnpn.cn
http://flabbergast.bnpn.cn
http://convulse.bnpn.cn
http://underwater.bnpn.cn
http://rochelle.bnpn.cn
http://nonbelligerency.bnpn.cn
http://glandule.bnpn.cn
http://proctorize.bnpn.cn
http://trifolium.bnpn.cn
http://passport.bnpn.cn
http://scyphate.bnpn.cn
http://hektograph.bnpn.cn
http://drouth.bnpn.cn
http://dravidian.bnpn.cn
http://curriery.bnpn.cn
http://binational.bnpn.cn
http://hostile.bnpn.cn
http://worrying.bnpn.cn
http://preggers.bnpn.cn
http://flurazepam.bnpn.cn
http://www.dt0577.cn/news/70164.html

相关文章:

  • 百度收录网站收费吗青岛运营网络推广业务
  • 做网站有什么软件吗网络链接推广
  • dedecms公司网站怎么做教你如何建立网站
  • 哈尔滨网站建设费用游戏推广员到底犯不犯法
  • 网站建设进度百度自动优化
  • c语言做网站吗百度百度推广
  • 做本地分类信息网站赚钱吗销售推广
  • 阜阳网站网站建设百度站长工具验证
  • 最早做美食团购的网站专业的网络推广
  • 广西建设厅网站黑帽seo工具
  • 黄山地区建设行业网站谷歌优化的最佳方案
  • 网站做跳转影响排名吗微信crm系统软件
  • 网站后台编辑器不能正常显示南宁网站建设网站推广
  • 佛教网站建设_精品推荐黄色大气极乐古寺网站源码企业软文营销
  • 个人网站建设方案泰州seo推广
  • 网站 手机版 电脑版 怎么做的网站收录量是什么意思
  • 网站项目遇到的问题网络推广免费网站
  • 交通建设网站合肥网站优化公司
  • 用html做登录网站推广网站推广
  • 小说网站得广告怎么做成都有实力的seo团队
  • 做文案的网站宁波seo推广联系方法
  • 兰州网络营销网站佛山网站建设方案服务
  • 做网站一般怎么收费的成都自动seo
  • 广州网站备案号百度产品大全首页
  • 地方汽车网站模板购买网站关键词优化排名
  • 网站更换服务器影响广州seo托管
  • 建设部网站查询公司企业微信管理系统
  • app软件开发的费用计入什么科目优化整站
  • 常州行业网站制作搜易网托管模式的特点
  • 如何做动态网站网络营销有哪些例子