SpringBoot introduction

With the popularity of dynamic language (Ruby, Groovy, Scala, Node. Js), Java development is particularly heavy: configuration, in a wide range of low development efficiency, the complex deployment process, and the third party technology integration is difficult. In the above environment, SpringBoot came into being. It uses the idea of “custom by configuration” to get projects up and running quickly. With SpringBoot, it is easy to create a standalone (running jar, embedded Servlet container) quasi-production-level Spring-framework-based project that requires little or no Spring configuration.

SpringBoot core features

  • It can be run independently as a JAR package. To run a SpringBoot project, you only need to run it through java-jar xx.jar
  • Embedded Servlet container, SpringBoot can choose Tomcat, Jetty, or Undertow so we don’t have to deploy the project as a WAR package
  • Simplify Maven configuration. SpringBoot provides a series of starter POMs to simplify Maven dependency loading
  • SpringBoot automatically configures beans for classes in jar packages based on the classes in the classpath, greatly reducing the configuration we need to use
  • SpringBoot provides monitoring of runtime projects based on HTTP, SSH, and Telnet
  • Instead of code generation, conditional annotations are implemented, which is a new feature in Spring4.x that does all of Spring’s configuration without any XML configuration

Build your first SpringBoot starter

  • Create a Jar project for Maven.

  • Pom.xml adds dependencies

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.zichen</groupId>
      <artifactId>springboot</artifactId>
      <version>0.0.1 - the SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>1.5.6. RELEASE</version>
      </parent>
    
      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
      </dependencies>
    
      <! -- Change JDK version -->
      <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
    
                  <configuration>
                      <source>1.8</source>
                      <target>1.8</target>
                  </configuration>
              </plugin>
          </plugins>
      </build>
    </project>
    Copy the code
  • How to write the FirstApplication class

    /** * Start class *@author Administrator
     *
     */
    @SpringBootApplication
    public class FirstApplication {
        public static void main(String[] args) { SpringApplication.run(FirstApplication.class, args); }}Copy the code
  • Run the test successfully

@EnableAutoConfigurationEnabling Automatic Configuration

@enableAutoConfiguration This annotation causes SpringBoot to automatically configure the configuration items of the project based on the jar packages that the project depends on. For example, if we add the spring-boot-starter-Web dependency, the project will introduce the SpringMVC dependency, and Spring Boot will automatically configure Tomcat and SpringMVC.

Spring Boot supports the following automatic configuration:

Disabling Automatic Configuration

@EnableAutoConfiguration(exclude={RedisAutoConfiguration.class})

Custom banner

  • Open website: custom banner website address

  • Copy the generated characters into a text file and name it banner.txt. Copy banner.txt to the resources directory of your project

  • Copy banner.txt to the resources directory of your project

    banner.txt

    //////////////////////////////////////////////////////////////////// // _ooOoo_ // // o8888888o // // 88" . "88 // // (|  ^_^ |) // // O\ = /O // // ____/`---'\____ // // .' \\| |// `. // // / \\||| : | | | / / / / / / / / _ | | | | | - : - | | | | | - / / / / / | | \ \ \ / / / | | / / / / | \ _ | '\ - /' | | / / / / \ \ __ ` - ` ___. / - / / / / / ___ `.. '/ -- -- \ `. ___ / / / /. ""' < `. ___ \ _ < | > _ / ___." "' > '. / / / / | | : ` - \ `.; ` \ _ / `; . ` / - ` : | | / / / / \ \ ` - \ _ __ \ / __ _ /. - ` / / / / / / = = = = = = = = ` - ____ ` - ___ \ _____ / ___ - ` _____. - '= = = = = = = = / / / / ` = - =' / / / / ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ / / / / the Buddha bless Never goes down Never BUG / / ////////////////////////////////////////////////////////////////////Copy the code
  • Start the project

Global configuration file

Spring Boot projects use a global configuration file, application.properties or application.yml, in the Resources directory or /config in the classpath.

  • Example Change the tomcat port to 8989

    server.port=8989
    Copy the code
  • The rule for entering the DispatcherServlet is *.html

    server.context-path=/hello
    Copy the code

  • Querying Full Configuration

    # BANNER
    banner.charset=UTF-8 # Banner file encoding.
    banner.location=classpath:banner.txt # Banner file location.
    banner.image.location=classpath:banner.gif # Banner image file location (jpg/png can also be used).
    banner.image.width= # Width of the banner image in chars (default 76)
    banner.image.height= # Height of the banner image in chars (default based on image height)
    banner.image.margin= # Left hand image margin in chars (default 2)
    banner.image.invert= # If images should be inverted for dark terminal themes (default false)
    
    # LOGGING
    logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
    logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
    logging.file= # Log file name. For instance `myapp.log`
    logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
    logging.path= # Location of the log file. For instance `/var/log`
    logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
    logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
    logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
    logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.
    Copy the code

The starter list of pom

SpringBoot provides us with a starter POM for most scenarios of enterprise development. As long as the starter POM required by the application scenario is used, the relevant technical configuration will be eliminated and Spring can be obtained

Boot provides automatic configuration beans for us.

Read from the definition configuration

  • Customize properties in the application.properties file: for example, book.author= Book. name=SpringBoot

  • Use @value (” ${book.author} “) to get the Value of the custom property

    # purple morning
    book.author=Purple morning
    book.name=SpringBoot
    Copy the code

Type-safe configuration

Injecting every custom configuration using @Value can be cumbersome in a project, many times when there are a lot of custom attributes, SpringBoot also provides type-safe configuration, A type-safe configuration is achieved by associating the properties in properties with the properties of a Bean via @ConfigurationProperties

  • Customize the properties in the application.properties file:

    For example,

    The book. The author = zhang SAN book. Name = SpringBootCopy the code
  • @ConfigurationProperties(prefix=”book”)

Profile configuration

Profiles support different configurations for different environments. The global Profile configuration uses application-*.properties

  • Application-prod. properties Production mode configuration

  • Application-sit. properties Online mode configuration

  • Application-dev.properties Configuration of the development mode

Specify the active Profile by setting spring.profiles.active=dev in application.properties.

spring.profiles.active=dev
Copy the code

Principle of SpringBoot automatic configuration