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

福州微信营销网站建设深圳进出口贸易有限公司

福州微信营销网站建设,深圳进出口贸易有限公司,wordpress打开要10秒,wordpress页面如何显示分类Spring 工厂加载机制#xff0c;即 Spring Factories Loader#xff0c;核心逻辑是使用 SpringFactoriesLoader 加载由用户实现的类#xff0c;并配置在约定好的META-INF/spring.factories 路径下#xff0c;该机制可以为框架上下文动态的增加扩展。 该机制类似于 Java SPI… Spring 工厂加载机制即 Spring Factories Loader核心逻辑是使用 SpringFactoriesLoader 加载由用户实现的类并配置在约定好的META-INF/spring.factories 路径下该机制可以为框架上下文动态的增加扩展。 该机制类似于 Java SPI给用户提供可扩展的钩子从而达到对框架的自定义扩展功能。 核心实现类 SpringFactoriesLoader SpringFactoriesLoader 是 Spring 工厂加载机制的核心底层实现类。它的主要作用是 从 META-INF/spring.factories 路径下加载指定接口的实现类。该文件可能存在于工程类路径下或者 jar 包之内所以会存在多个该文件。 public static final String FACTORIES_RESOURCE_LOCATION META-INF/spring.factories; SpringFactoriesLoader loadFactories load 并从 FACTORIES_RESOURCE_LOCATION文件中实例化给定类型的工厂实现类。 spring.factories 文件必须采用 Properties 格式其中键是接口或抽象类的完全限定*名称值是逗号分隔的实现类名称列表。例如: 该文件的格式Key 必须为接口或抽象类的全限定名value 为 具体的实现类多个以 逗号分隔。类似如下配置 # Application Context Initializers org.springframework.context.ApplicationContextInitializer\ org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\ org.springframework.boot.context.ContextIdApplicationContextInitializer,\ org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\ org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer 从该文件中我们可以看到其中 ApplicationContextInitializer 为父类value为实现类以逗号分隔。 SpringFactoriesLoader 源码分析 Spring Boot 完成自动装配的核心之一就是工厂加载机制。我们以 Spring Boot 的自动装配为例来分析。如果要开启 Spring 的自动装配功能会使用 EnableAutoConfiguration 这个注解这个注解会 Import AutoConfigurationImportSelector 这个类。 Target(ElementType.TYPE) Retention(RetentionPolicy.RUNTIME) Documented Inherited AutoConfigurationPackage Import(AutoConfigurationImportSelector.class) public interface EnableAutoConfiguration { AutoConfigurationImportSelector 中有一个方法就是加载 EnableAutoConfiguration 为 key 的实现配置类。 protected ListAutoConfigurationImportFilter getAutoConfigurationImportFilters() {return SpringFactoriesLoader.loadFactories(AutoConfigurationImportFilter.class,this.beanClassLoader) } SpringFactoriesLoader loadFactories 加载 所有以 factoryClass 为 Key 的实现类 public static T ListT loadFactories(ClassT factoryClass, Nullable ClassLoader classLoader) {// 省略部分前置判断和 logger 代码 ClassLoader classLoaderToUse classLoader;if (classLoaderToUse null) {classLoaderToUse SpringFactoriesLoader.class.getClassLoader();}//根据当前接口类的全限定名作为key从loadFactoryNames从文件中获取到所有实现类的全限定名ListString factoryNames loadFactoryNames(factoryClass, classLoaderToUse);ListT result new ArrayList(factoryNames.size());//实例化所有实现类并保存到 result 中返回。for (String factoryName : factoryNames) {result.add(instantiateFactory(factoryName, factoryClass, classLoaderToUse));}AnnotationAwareOrderComparator.sort(result);return result;} 调用 loadSpringFactories 从META-INF/spring.factories文件中进行加载 从文件中读取接口和实现类的逻辑返回 MapString, ListString private static MapString, ListString loadSpringFactories(Nullable ClassLoader classLoader) {MultiValueMapString, String result cache.get(classLoader);if (result ! null) {return result;}try {//FACTORIES_RESOURCE_LOCATION -- META-INF/spring.factoriesEnumerationURL urls (classLoader ! null ?classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));//一Key多值 Map适合上文提到的一个接口多个实现类的情形。result new LinkedMultiValueMap();while (urls.hasMoreElements()) {URL url urls.nextElement();UrlResource resource new UrlResource(url);Properties properties PropertiesLoaderUtils.loadProperties(resource);for (Map.Entry?, ? entry : properties.entrySet()) {//以逗号进行分割得到List的实现类全限定名集合ListString factoryClassNames Arrays.asList(StringUtils.commaDelimitedListToStringArray((String) entry.getValue()));result.addAll((String) entry.getKey(), factoryClassNames);}}cache.put(classLoader, result);//返回return result;}catch (IOException ex) {throw new IllegalArgumentException(Unable to load factories from location [ FACTORIES_RESOURCE_LOCATION ], ex);} } 总结 上面通过以 Spring Boot 的自动装配为例我们分析了 Spring 工厂加载机制的整个过程重点分析了SpringFactoriesLoader类。通过这样的机制我们可以十分的方便的为框架提供各式各样的扩展插件我们可以自己定义自己的组件的自动装配配置类然后通过工厂加载机制让 Spring 进行加载并得到自动装配。 工厂加载机制的应用 ApplicationContextInitializer ApplicationContextInitializer 是在 Spring Boot 或者 Spring Mvc 启动过程中调用的。具体时机为Spring 应用上下文 refresh 之前(调用 refresh 方法)。 ApplicationContextInitializer 主要提供应用上下文未refresh之前的扩展这时候可以对 ConfigurableApplicationContext 进行一些扩展处理等。 自定义一个类实现 ApplicationContextInitializer 并重写 initialize 方法 Order(Ordered.HIGHEST_PRECEDENCE) public class HelloWorldApplicationContextInitializer implements ApplicationContextInitializer {Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {System.out.println( applicationContext: applicationContext.getId());} } 启动 Spring Boot 程序我们可以看到在 refresh 之前会在控制台打印上面这句话。 另外的实现方式 application.properties添加配置方式context.initializer.classescom.maple.spring.initializer.AfterHelloWorldApplicationContextInitializer 对于这种方式是通过 DelegatingApplicationContextInitializer 这个初始化类中的 initialize 方法获取到 application.properties 中 context.initializer.classes 对应的实现类并对该实现类进行加载。 3.在 SpringApplication 中直接添加 public static void main(String[] args) {new SpringApplicationBuilder(SpringBootDemo3Application.class).initializers(new AfterHelloWorldApplicationContextInitializer()).run(args);} } Spring Boot 使用 工厂机制加载 ApplicationListener 实现类 # Application Listeners org.springframework.context.ApplicationListener\ org.springframework.boot.ClearCachesApplicationListener,\ org.springframework.boot.builder.ParentContextCloserApplicationListener,\ org.springframework.boot.context.FileEncodingApplicationListener,\ org.springframework.boot.context.config.AnsiOutputApplicationListener,\ org.springframework.boot.context.config.ConfigFileApplicationListener,\ org.springframework.boot.context.config.DelegatingApplicationListener,\ org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\ org.springframework.boot.context.logging.LoggingApplicationListener,\ org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener 总结 工厂加载机制是 Spring 动态加载实现类的一种方式提前在扩展类中写好对应自动配置类我们可以完成自动装配的效果。Spring Boot 自动装配模块其中的loader 自动配置实现类就是基于此实现的。Spring Boot 的一些新特性几乎用到的都是 Spring Framework 的核心特性。因此学习 Spring Boot 归根结底就是学习 Spring Framework 核心。它是所有 Spring 应用的基石所以我们应该从上至下由浅入深来进行学习和分析。 转载于:https://www.cnblogs.com/leihuazhe/p/9751836.html
http://www.pierceye.com/news/330971/

相关文章:

  • 自己建的网站打开的特别慢盐城网站建设效果
  • 专业建站报价wordpress这软件怎么搜索
  • 德国网站建设电工培训内容
  • 织梦手机wap网站标签调用外贸网站建设公司如何
  • 在那里能找到网站泰安公司网站开发
  • 大兴区企业网站建设我们网站的优势
  • 呼伦贝尔市建设局网站关键词如何排名在首页
  • 网站带后台模板网站的建设宗旨
  • 深圳网站建设php专门查企业的网站
  • 做问卷调查的网站有啥世界比分榜
  • 网站301定向深圳电梯广告制作公司网站
  • 个人网站做推广系统开发师
  • 智能建站的优势和不足app注册推广拉人
  • 做网站用软件网站制作怎么创业
  • 解放碑电子商务网站建设网站建设英文如何表达
  • 长春好的做网站公司有哪些网站建设标准
  • 公司网站首页大图怎么做台州网站制作定制
  • 网站建设公司软件开发浅谈网站建设开发
  • 松江网站开发培训课程海外域名注册商
  • 智慧景区网站服务建设线下课程seo
  • 做3个网站需要多大的服务器做地铁建设的公司网站
  • 深圳app网站建设哪家好广西桂林
  • 网站开发及上线过程网站建设备案策划书
  • 杭州九鸿科技网站开发网站模板 素材
  • 网站建设网站软件有哪些wordpress如何输入拼音
  • 昆山网站建设哪家便宜简单的模板网站
  • 做图标得英文网站wordpress写代码插件
  • 网站网页设计案例wordprees可以做棋类网站吗
  • 天河区门户网站官网小学生一分钟新闻播报
  • 漯河网站建设lhwzzz网络服务器机柜