Spring-boot summary

Spring-boot summary

 

 

       The purpose of Spring-boot is to help developers quickly build the Spring framework and help developers quickly start the web container. Spring-boot inherits the excellent genes of the original Spring framework, and Spring-boot simplifies the process of using Spring.

 

 

Core functions

 

1. Change from a large number of XML configurations to JavaConfig, which looks clearer, because the classes are separated and there is no need to configure web.xml. When javaConfig, information needs to be injected into beans, such as: interceptor, filter, DispatcherServlet configuration

 

2. Embedded Tomcat (default) and Jetty, as long as there is JDK, the web service can be started

 

3. Provide starter to simplify Maven configuration. As long as spring-boot-starter-web is used, many dependent packages can be added automatically, such as: webmvc, core, tomcat, annotation

 

4. Development supports hot deployment, spring loaded

 

5. There is only one global application.properties file, here is the overall configuration of the entire project

 

6. Logback uses logback by default. Different logging.levels can be activated according to the configuration file. Logback can have its own xml for configuration activation, log formatting, log file size, and compression and packaging.

 

 

 

 

Spring-boot automatic configuration principle

 

       Here we still need to look at the annotation @SpringBootApplication. Its implementation is a composite annotation, the core of which is provided by @EnableAutoConfiguration, which has a SpringFactoriesLoader.loadFactoryNames method to scan which classes to automatically configure in spring.factories.

 

       Therefore, it is very important to choose spring-boot-starter-web in the pom. Adding this dependency is equivalent to adding many common dependencies of spring.

 

 

 

 

How Spring-boot-starter works

 

1. When Spring-boot starts, it scans all dependent jar packages to find the jar that contains the spring.factories file

2. 根据spring.factories配置,加载自动加载的类

3. 根据 @Conditional注解的条件,进行自动配置并将Bean注入Spring Context

 

 

 

 

自定义Spring-boot-starter注意事项

 

1. springboot默认scan的包名是其main类所在的包名。如果引入的starter包名不一样,需要自己添加scan。

@ComponentScan(basePackages = {"com.xixicat.demo","com.xixicat.sms"})

 

 

2. 对于starter中有feign的,需要额外指定

@EnableFeignClients(basePackages = {"com.xixicat.sms"})

 

 

3. 对于exclude一些autoConfig

@EnableAutoConfiguration(exclude = {MetricFilterAutoConfiguration.class})

 

 

 

 

 

对Spring-boot的认识

 

       spring Boot是一个开源框架,它可用于创建可执行的Spring应用程序,采用了习惯优于配置的方法。此框架的神奇之处在于@EnableAutoConfiguration注解,此注释自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找。

 

 

@Enable*注释

 

@Enable*注释并不是新发明的注释,早在Spring 3框架就引入了这些注释,用这些注释替代XML配置文件。

 

  • @EnableTransactionManagement:它能够声明事务管理
  • @EnableWebMvc:它能启用Spring MVC
  • @EnableScheduling:它可以初始化一个调度器。 

 

 

属性映射

 

下面看MongoProperties类,它是一个Spring Boot属性映射的例子:

 

@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties {

    private String host;
    private int port = DBPort.PORT;
    private String uri = "mongodb://localhost/test";
    private String database;

    // ... getters/ setters omitted
}

 

 @ConfigurationProperties注释将POJO关联到指定前缀的每一个属性。例如,spring.data.mongodb.port属性将映射到这个类的端口属性。 

 

强烈建议Spring Boot开发者使用这种方式来删除与配置属性相关的瓶颈代码。

 

 

 

@Conditional注释

 

Spring Boot的强大之处在于使用了Spring 4框架的新特性:@Conditional注释,此注释使得只有在特定条件满足时才启用一些配置。 

 

在Spring Boot的org.springframework.boot.autoconfigure.condition包中说明了使用@Conditional注释能给我们带来什么,下面对这些注释做一个概述:

 

  • @ConditionalOnBean
  • @ConditionalOnClass
  • @ConditionalOnExpression
  • @ConditionalOnMissingBean
  • @ConditionalOnMissingClass
  • @ConditionalOnNotWebApplication
  • @ConditionalOnResource
  • @ConditionalOnWebApplication

 

 

应用程序上下文初始化器

 

       spring.factories还提供了第二种可能性,即定义应用程序的初始化。这使得我们可以在应用程序载入前操纵Spring的应用程序上下文ApplicationContext。 

 

       特别是,可以在上下文创建监听器,使用ConfigurableApplicationContext类的addApplicationListener()方法。 

 

       AutoConfigurationReportLoggingInitializer监听到系统事件时,比如上下文刷新或应用程序启动故障之类的事件,Spring Boot可以执行一些工作。这有助于我们以调试模式启动应用程序时创建自动配置的报告。 

 

      要以调试模式启动应用程序,可以使用-Ddebug标识,或者在application.properties文件这添加属性debug= true。

 

 

 

 

学习

http://tengj.top/2017/04/24/springboot0/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326123011&siteId=291194637