建立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文件 ...
随机推荐
- Dubbo源码剖析三之服务注册过程分析
Dubbo源码剖析二之注册中心 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中对注册中心进行了简单的介绍,对Dubbo整合Zookeeper链接源码进行了详细分析.本文接着对服务注册过 ...
- Linux系列——常规基础操作
1.配置IP a.若安装桌面版系统,直接GUI画面配置,操作直观.简单 b.若安装非桌面版系统,进行如下配置: 1).ifconfig命令(临时配置,重启后失效) ifconfig eth ...
- Java笔记——选择语
Java笔记--选择语句 1. if语句 规律: 1. 首先计算表达式的值. 2. 若表达式为真,则执行对应语句,为假则不执行. 第一种: if(表达式) 语句;//多个语句可用{} 例如 ...
- 【windows 访问控制】十、词汇列表和对应C#类、枚举、命名空间
principals:主体 主体包含标识(identity 对用来来说就是用户名,对程序来说就是SID)和用户角色(role 对用户来说就是 组名 对程序来说就是组SID)subject:主体.主语i ...
- python中的list, dict, tuple以及collections模块的基本用法
1.关于list的一些基本用法 # 创建没有初值的列表 list1=[] # 创建有初值的列表 list2=['this','is','a','list'] # 创建给定长度但初值不确定的列表 lis ...
- vmware启动报错:Failed to load SELinux policy. Freezing
修改 : SELINUX=disabled 正确 误修改: SELINUXTYPE=disabled 错误 导致无法开机 错误结果 重启后 机器就报 Failed to load SELi ...
- Linux概述及简单命令
Linux概述及简单命令 转自https://www.cnblogs.com/ayu305/p/Linux_basic.html 一.准备工作 1.环境选择:VMware\阿里云服务器 2.Linux ...
- linux目录跳转的好武器z.sh
转至:https://blog.csdn.net/molaifeng/article/details/14123123 中午刷微博时看到一篇有关z.sh的介绍. 众所周知,在linux系统中进入目录都 ...
- .NET WebApi使用Swagger
1.新建WebApi 项目 2.引用Swagger 包 3.创建项目XML注释文档 在项目App_Start文件夹下的SwaggerConfig.cs类中加入 c.IncludeXmlComments ...
- .Net/C#分库分表高性能O(1)瀑布流分页
.Net/C#分库分表高性能O(1)瀑布流分页 框架介绍 依照惯例首先介绍本期主角:ShardingCore 一款ef-core下高性能.轻量级针对分表分库读写分离的解决方案,具有零依赖.零学习成本. ...