Spring Boot 运行原理,自动配置

我们可以先看看这段代码发生了什么事情

SpringApplication.run(App.class, args);


在创建SpringApplication的时候初始化了一些ApplicationContextApplicationListener

主要通过getSpringFactoriesInstances方法来实现


上面的SpringFactoriesLoader.loadFactoryNames方法看这里


下面我们可以查看下spring.factories文件,spring-boot-autoconfigure spring-bootjar包中都有


我们可以先看看Spring-boot里面的文件


SpringApplication创建,初始化了上述的 Application ContextApplication Listeners


通过spring.factories文件拿到一系列的ContextListener之后 执行run方法

run方法会从spring.factories文件中获取到run listener,然后在spirng boot 执行到各个阶段时执行Listener事件和Context事件

所以,所谓的SpringApplicationRunListeners实际上就是在SpringApplication对象的run方法执行的不同阶段,去执行一些操作,并且这些操作是可配置的。

Spring boot总共有这些事件类型


Spring 事件体系

http://blog.csdn.net/caihaijiang/article/details/7460888


看下createAndRefreshContext方法


applyInitializers方法其中

DelegatingApplicationContextInitializer读取context.initializer.classes配置,这些类都是实现了ApplicationContextInitializer接口的类,读取之后执行initialize方法,所以我们在这里可以自己扩展一些东西

 

这里有一些listenercontext的功能图


这里面代码看的晕乎乎的,都不知道理解的对不对,希望不会对你们产生误导

参考资料

http://blog.csdn.net/liaokailin/article/category/5765237

http://zhaox.github.io/java/2016/03/22/spring-boot-start-flow

http://blog.csdn.net/u011179993/article/details/51475732


自动配置

Spring Boot关于自动配置的源码在spring-boot-autoconfigure.


上面的这些东西主要是靠condition包下面的注解来根据不同的条件自动创建Bean


这些注解都是组合了@Conditional元注解,只是使用了不同的条件

 

我们可以查看下@ConditionalOnWebApplication这个注解


这个注解使用的条件是OnWebApplicationCondition这个类

  1. package org.springframework.boot.autoconfigure.condition;
  2. import org.springframework.context.annotation.Condition;
  3. import org.springframework.context.annotation.ConditionContext;
  4. import org.springframework.core.Ordered;
  5. import org.springframework.core.annotation.Order;
  6. import org.springframework.core.type.AnnotatedTypeMetadata;
  7. import org.springframework.util.ClassUtils;
  8. import org.springframework.util.ObjectUtils;
  9. import org.springframework.web.context.WebApplicationContext;
  10. import org.springframework.web.context.support.StandardServletEnvironment;
  11. /**
  12. * {@link Condition} that checks for the presence or absence of
  13. * {@link WebApplicationContext}.
  14. *
  15. * @author Dave Syer
  16. * @see ConditionalOnWebApplication
  17. * @see ConditionalOnNotWebApplication
  18. */
  19. @Order(Ordered.HIGHEST_PRECEDENCE + 20)
  20. class OnWebApplicationCondition extends SpringBootCondition {
  21. private static final String WEB_CONTEXT_CLASS = "org.springframework.web.context."
  22. + "support.GenericWebApplicationContext";
  23. @Override
  24. public ConditionOutcome getMatchOutcome(ConditionContext context,
  25. AnnotatedTypeMetadata metadata) {
  26. boolean webApplicationRequired = metadata
  27. .isAnnotated(ConditionalOnWebApplication.class.getName());
  28. //判断是否是web环境,并获取结果
  29. ConditionOutcome webApplication = isWebApplication(context, metadata);
  30. if (webApplicationRequired && !webApplication.isMatch()) {
  31. return ConditionOutcome.noMatch(webApplication.getMessage());
  32. }
  33. if (!webApplicationRequired && webApplication.isMatch()) {
  34. return ConditionOutcome.noMatch(webApplication.getMessage());
  35. }
  36. return ConditionOutcome.match(webApplication.getMessage());
  37. }
  38. private ConditionOutcome isWebApplication(ConditionContext context,
  39. AnnotatedTypeMetadata metadata) {
  40. //判断GenericWebApplicationContext是否在类路径中
  41. if (!ClassUtils.isPresent(WEB_CONTEXT_CLASS, context.getClassLoader())) {
  42. return ConditionOutcome.noMatch( "web application classes not found");
  43. }
  44. //容器中是否有名为session的scope
  45. if (context.getBeanFactory() != null) {
  46. String[] scopes = context.getBeanFactory().getRegisteredScopeNames();
  47. if (ObjectUtils.containsElement(scopes, "session")) {
  48. return ConditionOutcome.match( "found web application 'session' scope");
  49. }
  50. }
  51. //当前容器的enviroment是否为StandardServletEnviroment
  52. if (context.getEnvironment() instanceof StandardServletEnvironment) {
  53. return ConditionOutcome
  54. .match( "found web application StandardServletEnvironment");
  55. }
  56. //当前容器的resourceLoader是否是WebApplicationContext
  57. if (context.getResourceLoader() instanceof WebApplicationContext) {
  58. return ConditionOutcome.match( "found web application WebApplicationContext");
  59. }
  60. return ConditionOutcome.noMatch( "not a web application");
  61. }
  62. }

最终通过ConditionOutcome返回是否web项目,还有判断结果


自定义starter pom

自己实现一个简单的例子,当某个类存在的时候,自动配置这个Bean,并且可以讲这个属性在application.properties中配置

 

新建一个maven项目

Pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0 </modelVersion>
  4. <groupId>com.ibigsea </groupId>
  5. <artifactId>spring-boot-starter-hello </artifactId>
  6. <version>0.0.1-SNAPSHOT </version>
  7. <packaging>jar </packaging>
  8. <name>spring-boot-starter-hello </name>
  9. <url>http://maven.apache.org </url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.springframework.boot </groupId>
  16. <artifactId>spring-boot-autoconfigure </artifactId>
  17. <version>1.3.3.RELEASE </version>
  18. </dependency>
  19. </dependencies>
  20. </project>

Hello.java

  1. package com.ibigsea.spring_boot_starter_hello;
  2. /**
  3. * 通过application.properties的hello.msg来配置,默认为world
  4. * @author bigsea
  5. *
  6. */
  7. public class Hello {
  8. private String msg;
  9. public String sayHello() {
  10. return "hello " + msg;
  11. }
  12. public String getMsg() {
  13. return msg;
  14. }
  15. public void setMsg(String msg) {
  16. this.msg = msg;
  17. }
  18. }

HelloProperties.java

  1. package com.ibigsea.spring_boot_starter_hello;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. /**
  4. * 属性配置,Spring boot 自身的自动配置
  5. * @author bigsea
  6. *
  7. */
  8. @ConfigurationProperties(prefix= "hello")
  9. public class HelloProperties {
  10. private static final String MSG = "world";
  11. private String msg = MSG ;
  12. public String getMsg() {
  13. return msg;
  14. }
  15. public void setMsg(String msg) {
  16. this.msg = msg;
  17. }
  18. }

HelloAutoConfiguration.java

  1. package com.ibigsea.spring_boot_starter_hello;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  4. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  6. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. @Configuration
  10. @EnableConfigurationProperties(HelloProperties.class) //开启属性注入,通过@autowired注入
  11. @ConditionalOnClass(Hello.class) //判断这个类是否在classpath中存在
  12. //当设置hello=enabled的情况下,如果没有设置则默认为true,即条件符合
  13. @ConditionalOnProperty(prefix= "hello", value= "enabled", matchIfMissing = true)
  14. public class HelloAutoConfiguration {
  15. @Autowired
  16. private HelloProperties helloProperties;
  17. @Bean //使用java配置方式配置这个类
  18. @ConditionalOnMissingBean(Hello.class) //容器中如果没有Hello这个类,那么自动配置这个Hello
  19. public Hello hello() {
  20. Hello hello = new Hello();
  21. hello.setMsg(helloProperties.getMsg());
  22. return hello;
  23. }
  24. }

并且要添加spring.factories

  1. # Auto Configure
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. com.ibigsea.spring_boot_starter_hello.HelloAutoConfiguration

整个项目结构


好了,到这里我们就完成了一个starter项目了,下面自己测试下

Pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0 </modelVersion>
  4. <groupId>com.ibigsea </groupId>
  5. <artifactId>test-starter </artifactId>
  6. <version>0.0.1-SNAPSHOT </version>
  7. <packaging>jar </packaging>
  8. <name>test-starter </name>
  9. <url>http://maven.apache.org </url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  12. <boot.version>1.3.3.RELEASE </boot.version>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.ibigsea </groupId>
  17. <artifactId>spring-boot-starter-hello </artifactId>
  18. <version>0.0.1-SNAPSHOT </version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot </groupId>
  22. <artifactId>spring-boot-starter-web </artifactId>
  23. <version>${boot.version} </version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot </groupId>
  27. <artifactId>spring-boot-starter-test </artifactId>
  28. <version>${boot.version} </version>
  29. <scope>test </scope>
  30. </dependency>
  31. </dependencies>
  32. </project>

App.java

  1. package com.ibigsea.test_starter;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.ibigsea.spring_boot_starter_hello.Hello;
  8. @SpringBootApplication
  9. @RestController
  10. public class App {
  11. @Autowired
  12. private Hello hello;
  13. @RequestMapping( "/")
  14. public String index() {
  15. return hello.sayHello();
  16. }
  17. public static void main(String[] args) {
  18. SpringApplication.run(App.class, args);
  19. }
  20. }

application.properties

hello.msg=bigsea

运行结果





开启dubug模式 可以看到自动配置信息

猜你喜欢

转载自blog.csdn.net/gududedabai/article/details/80829084