谷歌网站地图生成,网站优化如何做pc指数,手机能建设网站,如何给局域网 做网站一、SpringBootConfiguration注解的作用 SpringBootApplication注解是SpringBoot项目的核心注解,加在启动引导类上。点击进去可以发现SpringBootApplication注解是一个组合注解。其中SpringBootConfiguration和EnableAutoConfiguration是由Spring提供的,剩下的注解是由JDK提供的…一、SpringBootConfiguration注解的作用 SpringBootApplication注解是SpringBoot项目的核心注解,加在启动引导类上。点击进去可以发现SpringBootApplication注解是一个组合注解。其中SpringBootConfiguration和EnableAutoConfiguration是由Spring提供的,剩下的注解是由JDK提供的。对于SpringBoot项目,我们重点分析SpringBootConfiguration和EnableAutoConfiguration注解。 1.1、SpringBootConfiguration表示启动引导类是一个配置类 点击SpringBootConfiguration注解进去可以发现SpringBootConfiguration注解也是一个组合注解,并且继承了Configuration注解,说明SpringBootApplication注解间接继承了
Configuration注解。 Configuration注解又加在了启动引导类上面,说明启动引导类是一个配置类,配置类中加了Bean注解的方法的返回值会放入Spring的容器中。 代码演示:
启动引导类中添加Bean注解的方法
package com.itboy;import com.itboy.Import.MyImportSeletor;
import com.itboy.pojo.User;
import org.example.config.BrandAutoConfiguration;
import org.example.config.UserAutoConfiguration;
import org.example.pojo.Brand;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;/*** */
SpringBootApplicationpublic class SpringBootPlusApplication {public static void main(String[] args) {ConfigurableApplicationContext app SpringApplication.run(SpringBootPlusApplication.class, args);User user app.getBean(User.class);System.out.println(user user);}Beanpublic User testUser(){return new User();}}运动程序,发现可以从容器中获得User对象说明添加了Bean注解的方法的返回值放到了Spring容器中。 所以SpringBootConfiguration注解的第一个作用就是将启动引导类变成配置类。 1.2、 SpringBootConfiguration可以让测试类能够找到启动引导类 注释掉启动引导类上面的SpringBootApplication注解,然后启动SpringBootTest测试类中的测试方法,会发现报错。 Unable to find a SpringBootConfiguration
说明 SpringBootTest测试类中的测试方法会去找SpringBootConfiguration注解。
此时需要手动指定启动引导类才能解决报错问题。 1.3、总结 SpringBootConfiguration注解的作用有:
1、表示启动引导类是一个配置类,因为相当于SpringBootApplication注解间接继承了Configuration注解。 2、 让测试类能够找到启动引导类,具体的看1.2中的内容。 二、Import注解的作用 通过Import注解中的导入器我们可以指定配置类。
我们在pom.xml配置文件中添加创建的模块 创建的模块结构如下图: 创建的模块中存在两个配置类UserAutoConfiguration和BrandAutoConfiguration 创建的模块和我们主模块之间的位置关系: 所以创建的模块肯定不在spring_boot项目启动引导类的包及其子包中。 所以在spring_boot项目的容器对象中肯定不能获取到创建的模块中的User和brand对象。 但是在启动引导类上添加Import注解就能获取到User和brand对象。 通过Import注解中的导入器能指定配置类有哪些,Import注解中的值相当于导入器,这里的MyImportSeletor.class是我自定义的导入器。 导入器需要实现ImportSelector接口,并重写selectImports方法,返回值为字符串数组,里面的值为配置类的全限定名,通过返回值就能确定配置类有哪些。 再找到配置类中加了Bean注解的方法,方法的返回值就会放入spring的容器中。就完成了自动装配的工程。 总结
1、Import注解可以通过导入器指定配置类。
2、Import注解中的值是导入器,通过导入器我们可以指定配置类。
3、配置类中加了Bean注解的方法的返回值就会放到spring容器中从而实现自动装配。 三、EnableAutoConfiguration注解的作用 EnableAutoConfiguration注解是组合注解,继承了Import注解。 通过二中分析我们知道可以通过Import注解中的导入器找到配置类,这里的AutoConfigurationImportSelector.class就是SpringBoot定义的导入器所以继续点击导入器。 发现AutoConfigurationImportSelector.class导入器中存在selectImports方法,并且方法返回值为字符串数组,与二中刚才的分析保持一致,所以这个selectImports方法就是指定配置类。 if的条件判断是返回没有导入的情况,所以else里面的逻辑才是指定配置类。 接着点击selectImports方法中的getAutoConfigurationEntry方法,再点击getAutoConfigurationEntry方法中的getCandidateConfigurations方法 接着点击 getCandidateConfigurations方法中的loadFactoryNames方法 接着点击loadFactoryNames方法中的loadSpringFactories方法 最后发现读取的是META-INF文件夹里面的spring.factorie文件 总结:
1、 EnableAutoConfiguration注解的作用是完成SpringBoot项目的自动装配。
2、会去读取META-INF文件夹里面的spring.factorie文件。
3、spring.factorie文件中定义了指定的配置类,找到配置类,并把配置类中加了Bean注解的方法的返回值放入Spring容器就完成了自动装配。 四、自动装配的条件选择
4.1、SpringBoot自带的Starter和第三方Starter SpringBoot提供的Starter是以spring-boot-starter开头的,例如spring-boot-starter-web和spring-boot-starter-test 第三方提供的starter一般是以spring-boot-starter为结尾的。例如:mybatis-spring-boot-starter 4.2、通过在External Libraries中找到对应的jar包 可以发现第三方Starter都会有 META-INF文件夹,并且META-INF文件夹里面肯定也会有spring.factorie文件。下面是mybatis的spring.factorie文件里面的内容。 org.springframework.boot.autoconfigure.EnableAutoConfiguration是key值,固定的写法。里面的value值定义了配置类,mybatis定义的配置类有:
MybatisLanguageDriverAutoConfiguration
MybatisAutoConfiguration
MybatisDependsOnDatabaseInitializationDetector 4.3、ConditionalOn.....注解决定了哪些方法返回值放入Spring容器中 打开4.2中找到的MybatisLanguageDriverAutoConfiguration配置类。 可以发现 MybatisLanguageDriverAutoConfiguration类肯定被Configuration注解定义了,那是不是这个类所有加了Bean注解的方法的返回值都会放入Spring容器中完成自动装配呢 可以发现MybatisLanguageDriverAutoConfiguration类中的很多方法上面加了类似ConditionalOnClass、ConditionalOnMissingBean等注解,都是限制条件,所以并不是所有的加了Bean注解的方法的返回值都会放入Spring容器中。 总结:
1、 指定的配置类中并不是所有的加了Bean注解的方法的返回值都会放入Spring容器中。
2、 ConditionalOnClass、ConditionalOnMissingBean等注解,都是限制条件。 五、自动装配原理总结 通过EnableAutoConfiguration注解,会去找到第三方jar包中META-INF文件夹里面的spring.factorie文件。 spring.factorie文件里面指定了配置类,再找到配置类中加了Bean注解的方法,再找到能通过ConditionalOnClass、ConditionalOnMissingBean等注解的限制条件后,就会将方法返回值放入Spring的容器中。
文章转载自: http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.bplqh.cn.gov.cn.bplqh.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.zkgpg.cn.gov.cn.zkgpg.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.sdecsd.cn.gov.cn.sdecsd.cn http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.swzpx.cn.gov.cn.swzpx.cn http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.hbdqf.cn.gov.cn.hbdqf.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.snmsq.cn.gov.cn.snmsq.cn http://www.morning.smqjl.cn.gov.cn.smqjl.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.ghphp.cn.gov.cn.ghphp.cn http://www.morning.rfzbm.cn.gov.cn.rfzbm.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.njfgl.cn.gov.cn.njfgl.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.lxmks.cn.gov.cn.lxmks.cn http://www.morning.wdrxh.cn.gov.cn.wdrxh.cn http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.lgsfb.cn.gov.cn.lgsfb.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.hnmbq.cn.gov.cn.hnmbq.cn http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.21r000.cn.gov.cn.21r000.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn http://www.morning.tbcfj.cn.gov.cn.tbcfj.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.ypzr.cn.gov.cn.ypzr.cn http://www.morning.jlgjn.cn.gov.cn.jlgjn.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.yhywx.cn.gov.cn.yhywx.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.khxwp.cn.gov.cn.khxwp.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.nbybb.cn.gov.cn.nbybb.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.rkzk.cn.gov.cn.rkzk.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.ysllp.cn.gov.cn.ysllp.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.tzzfy.cn.gov.cn.tzzfy.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.mmxt.cn.gov.cn.mmxt.cn http://www.morning.kzpy.cn.gov.cn.kzpy.cn