建立META-INF/spring.factories文件的意义何在
平常我们如何将Bean注入到容器当中
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration { @Autowired
HelloProperties helloProperties; @Bean
public HelloService helloService() {
HelloService service = new HelloService();
service.setHelloProperties( helloProperties );
return service;
}
}
一般就建立配置文件使用@Configuration,里面通过@Bean进行加载bean
或者使用@Compont注解在类上进行类的注入
注意:
在我们主程序入口的时候:
@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 {
里面注解@EnableAutoConfiguration
@ComponentScan注解指扫描@SpringBootApplication注解的入口程序类所在的basepackage下的
所有带有@Component注解的bean,从而注入到容器当中。
但是
如果是加入maven坐标依赖的jar包,就是项目根目录以外的Bean是怎么添加的??
这个时候注解@EnableAutoConfiguration的作用就来了
导入了AutoConfigurationImportSelector这个类:
这个类里面有一个方法
/**
* Return the auto-configuration class names that should be considered. By default
* this method will load candidates using {@link SpringFactoriesLoader} with
* {@link #getSpringFactoriesLoaderFactoryClass()}.
* @param metadata the source metadata
* @param attributes the {@link #getAttributes(AnnotationMetadata) annotation
* attributes}
* @return a list of candidate configurations
*/
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;
}
@EnableAutoConfiguration注解来注册项目包外的bean。而spring.factories文件,则是用来记录项目包外需要注册的bean类名
为什么需要spring.factories文件,
因为我们整个项目里面的入口文件只会扫描整个项目里面下的@Compont @Configuration等注解
但是如果我们是引用了其他jar包,而其他jar包只有@Bean或者@Compont等注解,是不会扫描到的。
除非你引入的jar包没有Bean加载到容器当中
所以我们是通过写/META-INF/spring.factories文件去进行加载的。
建立META-INF/spring.factories文件的意义何在的更多相关文章
- 关于META-INF下的spring.factories文件
spring.factories 文件是springboot提供的一种实例化bean方式 org.springframework.boot.autoconfigure.EnableAutoConfig ...
- Spring.factories扩展机制
和Java SPI的扩展机制类似,Spring Boot采用了spring.factories的扩展机制,在很多spring的starter 包中都可以找到,通过在 META-INF/spring.f ...
- java SPI & spring factories
SPI 全称为 (Service Provider Interface) ,是JDK内置的一种服务提供发现机制.SPI是一种动态替换发现的机制, 比如有个接口,想运行时动态的给它添加实现,你只需要添加 ...
- spring.factories
在Spring Boot中有一种非常解耦的扩展机制:Spring Factories.这种扩展机制实际上是仿照Java中的SPI扩展机制来实现的. Java SPI机制SPI的全名为Service P ...
- spring.factories配置文件的工厂模式
在springboot的各个依赖包下,我们经常看到META-INF/spring.factories这个文件.spring.factories文件的内容基本上都是这样的格式: # Initialize ...
- [SpringBoot] 通过spring.factory文件来加载第三方的bean
在springboot的开发过程中,我们经常需要加载一些bean,如果bean使我们自己写的类,那很好办,加个@Component注解就搞定了,然后过程启动会扫描启动类所在的包及其子包,如果我 ...
- SpringBoot之spring.factories
组件提供者如何编写出仅需系统开发者进行包引入就可以对spring进行bean注入等操作? 其实在spring库中有提供自动化配置的库spring-boot-autoconfigure,我们只需要引 ...
- 注意:Spring Boot 2.7开始spring.factories不推荐使用了,接下来这么玩...
如果你是Spring Boot用户的话,一定有这样的开发体验,当我们要引入某个功能的时候,只需要在maven或gradle的配置中直接引入对应的Starter,马上就可以使用了,而不需要像传统Spri ...
- Linux中.a,.la,.o,.so文件的意义和编程实现
Linux中.a,.la,.o,.so文件的意义和编程实现 Linux下文件的类型是不依赖于其后缀名的,但一般来讲: .o,是目标文件,相当于windows中的.obj文件 ...
随机推荐
- 一文带你解读Spring5源码解析 IOC之开启Bean的加载,以及FactoryBean和BeanFactory的区别。
前言 通过往期的文章我们已经了解了Spring对XML配置文件的解析,将分析的信息组装成BeanDefinition,并将其保存到相应的BeanDefinitionRegistry中,至此Spring ...
- RTP包中timestamp的间隔问题
概述 近期在和同事调试G729的编解码库时碰到一个语音质量的问题,问题产生的原因和RTP包中的时间戳设置有关,特此记录下来. 问题现象,1001和1002账号注册在fs,媒体设置为G729并通过fs中 ...
- 攻防世界之Web_upload1
题目: 本题考查的是文件上传漏洞. 上传一句话木马. 桌面新建一个webshell.php文本文件,写入<?php @eval($_POST['pass']);?>保存. 点击浏览,选择 ...
- Java中的Unsafe在安全领域的一些应用总结和复现
目录 0 前言 1 基本使用 1.1 内存级别修改值 1.2 创建对象 1.3 创建VM Anonymous Class 2 利用姿势 2.1 修改值以关闭RASP等防御措施 2.2 创建Native ...
- RFC3918组转发矩阵测试——网络测试仪实操
一.简介 1.RFC3918简介 历史 · 在1999年3月成为正式标准 功能 · 评测网络互连设备或网络系统的性能 · 网络设备: 交换机,路由器- 内容 · 定义了一整套测试方法,为不同厂家的设备 ...
- scrapy初体验
1. 构建scrapy项目 scrapy startproject ['项目名'] 在spiders下生成文件 cd spiders scrapy genspider douban_spider [' ...
- BI报表系统该如何集成到其他系统呢?
近期小麦我经常收到很多用户的反馈,想知道Smartbi的报表能不能从微信/钉钉之类的直接跳转到已做好的报表页面?他们都希望通过这种方式尽可能地避免由于各个管理软件账号密码不同而造成的不便,能够在日常工 ...
- yapi安装 - windows
yapi官网https://hellosean1025.github.io/yapi/devops/index.html 简介 YApi 是高效.易用.功能强大的 api 管理平台,旨在为开发.产 ...
- Spring切换多个配置环境(dev,prod,test等)
默认有一个application.properties配置文件, 然后在resource目录下新建3个配置文件比如 application-dev.properties application-tes ...
- c++刷leetcode记录
#include<iostream> #include<sstream> #include<vector> std::vector<int> split ...