您好,欢迎访问一九零五行业门户网

java SpringBoot自动装配原理是什么

summarydetail传统的spring项目会有很多的配置文件,比如我们要使用redis,一般除了对应的依赖的jar包我们还需要在application.xml里面配置jedisconnectionfactory、jedispoolconfig、redistemplate。但是如果使用springboot的话,系统会根据pom.xml里面的jar包,自动生成这些类并且注入到ioc容器当中。
传统spring项目中需要配置
<bean id="jedisconnectionfactory" class="...jedisconnectionfactory"></bean><bean id="poolconfig" class="redis.clients.jedis.jedispoolconfig"></bean><bean id="redistemplate" class="org.springframework.data.redis.core.redistemplate"></bean>
而使用springboot的话,除了pom.xml引入相应的jar包外,只需要在application.properties配置对应的属性值即可
以redis举例从spring-boot-autoconfigure.jar/meta-inf/spring.factories中获取120多个默认功能配置类,其中包括redis的功能配置类redisautoconfiguration的全限定名,一般一个功能配置类围绕该功能,负责管理创建多个相关的功能类,比如redisautoconfiguration负责:
jedisconnectionfactory、redistemplate、stringredistemplate这3个功能类的创建
org.springframework.boot.autoconfigure.data.redis.redisautoconfiguration,\
redisautoconfiguration配置类生效的一个条件是@conditionalonclass :jedisconnection.class, redisoperations.class, jedis.class,所以会去classpath下去查找对应的class文件
@configuration@conditionalonclass({ jedisconnection.class, redisoperations.class, jedis.class })@enableconfigurationproperties(redisproperties.class)public class redisautoconfiguration {}
如果pom.xml有对应的jar包,就能匹配到对应依赖class:jedisconnection.class, redisoperations.class, jedis.class
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency>
匹配成功,这个功能配置类才会生效,同时会注入默认的属性配置类@enableconfigurationproperties(redisproperties.class)
@configurationproperties(prefix = "spring.redis")public class redisproperties { private int database = 0; private string url; private string host = "localhost"; private string password; private int port = 6379;
redis功能配置里面会根据条件生成最终的jedisconnectionfactory、redistemplate,条件就是ioc环境里面,没有用户自定义的
@conditionalonmissingbean(redisconnectionfactory.class)、redistemplate
@configuration@conditionalonclass({ jedisconnection.class, redisoperations.class, jedis.class })@enableconfigurationproperties(redisproperties.class)public class redisautoconfiguration { @bean @conditionalonmissingbean(redisconnectionfactory.class) public jedisconnectionfactory redisconnectionfactory() throws unknownhostexception { return applyproperties(createjedisconnectionfactory()); } @bean @conditionalonmissingbean(name = "redistemplate") public redistemplate<object, object> redistemplate( redisconnectionfactory redisconnectionfactory) throws unknownhostexception { redistemplate<object, object> template = new redistemplate<object, object>(); template.setconnectionfactory(redisconnectionfactory); return template; }}
最终创建好的默认装配类,会通过功能配置类里面的 @bean注解,注入到ioc当中
原理和结果分析通过各种注解,springapplication.run(application.class, args)在运行时,会读取spring-boot-autoconfigure.jar里面的spring.factories配置文件,配置文件中有所有自动装配类的配置类的classname,然后生成对应功能的configuration类,这些功能配置类要生效的话,会去classpath中找是否有该类的依赖类(也就是pom.xml必须有对应功能的jar包才行),然后配置类里再通过判断生成最后的功能类,并且配置类里面注入了默认属性值类,功能类可以引用并赋默认值。生成功能类的原则是自定义优先,没有自定义时才会使用自动装配类。
综上所述,要想自动装配一个类需要满足2个条件:
spring.factories里面有这个类的配置类(一个配置类可以创建多个围绕该功能的依赖类)
pom.xml里面需要有对应的jar包
整个过程的结果是两件事情:
根据各种判断和依赖,最终生成了业务需要的类并且注入到ioc容器当中了
自动装配生成的类赋予了一些默认的属性值
依赖的注解@springbootapplication:sb项目应用启动类的注解,其实是3个注解的组合:@springbootconfiguration、@enableautoconfiguration、@componentscan,其中在自动装配中起作用的是第二个
@enableautoconfiguration:表示sb应用启动自动装配的功能(包括加载对应的bean到ioc容器中,且根据默认配置对属性赋值)
@import(enableautoconfigurationimportselector.class):这个注解比较厉害,可以把没有注册到ioc中的bean强行注册到ioc中,表示启动自动配置功能需要引入enableautoconfigurationimportselector.class才行
@configuration
@conditionalonclass({ jedisconnection.class, redisoperations.class, jedis.class }):表示让redisautoconfiguration配置类起作用的话,必须有包含这些类的jar包才行
@enableconfigurationproperties(redisproperties.class):表示默认引用redisproperties.class里面的配置
@conditionalonmissingbean(redisconnectionfactory.class):这是个很厉害的注解,实现自动装配时自定义优先。表示如果用户没有自定义注入redisconnectionfactory.class类,才会使用默认的jedisconnectionfactory。
自动装配的过程通过各种注解实现了类与类之间的依赖关系,容器在启动的时候application.run,会调用enableautoconfigurationimportselector.class的selectimports方法(其实是其父类的方法)
selectimports方法最终会调用springfactoriesloader.loadfactorynames方法来获取一个全面的常用beanconfiguration列表
loadfactorynames方法会读取factories_resource_location(也就是spring-boot-autoconfigure.jar 下面的spring.factories),获取到所有的spring相关的bean的全限定名classname,大概120多个
selectimports方法继续调用filter(configurations, autoconfigurationmetadata);这个时候会根据这些beanconfiguration里面的条件,来一一筛选,最关键的是@conditionalonclass,这个条件注解会去classpath下查找,jar包里面是否有这个条件依赖类,所以必须有了相应的jar包,才有这些依赖类,才会生成ioc环境需要的一些默认配置bean
最后把符合条件的beanconfiguration注入默认的enableconfigurationpropertie类里面的属性值,并且注入到ioc环境当中
以上就是java springboot自动装配原理是什么的详细内容。
其它类似信息

推荐信息