Spring Boot Concept Knowledge

Source: Internet
Author: User

Turn http://rapharino.com/coder/Spring-Boot-Induction/

Spring Boot Induction

Posted in 2016-10-29 |    Categories in coder |      | Read Count

Spring boot simplifies spring-based application development and tries to encapsulate spring's dreaded configuration by creating a standalone, product-level spring application with minimal configuration. Spring boot makes spring applications more lightweight, automated, and provides out-of-the-box configuration for spring platforms and third-party libraries. Spring boot does not introduce any form of code generation technology, but instead leverages Spring4 's features and project building practices Excellent integrated application framework. So it's essentially Spring.

Goal
    • Provides a fundamentally faster and anywhere-to-get introductory experience for all spring development.
    • Out of the box, but you can get out of this way quickly by not using the default settings.
    • Provides a wide range of non-functional features commonly used in large projects, such as: Embedded server, security, indicators, health detection, and external configuration.
    • There is no redundant code generation, and no XML configuration is required.
Characteristics
    • Automatic configuration: Provides default configuration for commonly used features. To meet the most basic functionality.
    • Start-Up dependency: Provides a large number of extension libraries for each function.
    • Command line interface: Optional features, no need to build projects, to achieve springboot dynamic compilation and operation.
    • Actuator: Provides basic monitoring services that provide runtime visibility into the application's internal conditions. including
      • The Bean in context
      • Automatic configuration policy (conditional configuration)
      • Environment variables, System properties, configuration Properties, command-line arguments, and so on.
      • Thread state
      • HTTP Call Condition Logging
      • Memory usage, garbage collection, requests and other related metrics.
Quick Start

Spring Boot strongly conforms to the design paradigm of the configuration. Below is a Demo that implements a Web Service with minimal code.

MAVEN Build Project
123456789101112131415161718192021222324252627282930
<!--boot parent ---<parent><groupId>Org.springframework.boot</groupId><artifactId>Spring-boot-starter-parent</artifactId><version>1.3.2.RELEASE</version><relativePath/> <!--lookup parent from repository --</parent><dependencies>     <!--boot starter --<dependency><groupId>Org.springframework.boot</groupId><artifactId>Spring-boot-starter</artifactId></dependency><!--Web Starter ---<dependency><groupId>Org.springframework.boot</groupId><artifactId>Spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>Org.springframework.boot</groupId><artifactId>Spring-boot-maven-plugin</artifactId></plugin></plugins></build>
Writing code
12345678910111213
@RestController@EnableAutoConfiguration Public class App {@RequestMapping("/")String Home() {return "Hello world!"; }Public   static void main(string[] args) throws Exception {Springapplication.run (app.class, args); }}

If you use a browser to open localhost:8080, you should see the following output:

1
Hello world!
Start process

The implementation of Springapplication in Rp-core is similar to Springboot, which is mainly used for reference to its automatic configuration, ApplicationContext life cycle management function.

  1. XML or Java code implements the description of object construction and dependency bindings.

  2. Create Annotationconfigapplicationcontext (or Annotationconfigembeddedwebapplicationcontext) (error.)

  3. SpringFactoriesLoader.loadFactoryNames()Read the Classpath file below META-INF/spring.factories . The main include Applicationcontextinitializer,applicationlistener, and @configuration its related implementation class (annotation Class).

  4. Register Applicationcontextinitializer.applicationlistener for ApplicationContext. (Extensions provided by native Spring)

  5. Start the automatic configuration process

    1. springbootapplication Annotations with enableautoconfiguration annotations

      12345678910111213141516171819202122232425
      @Target(Elementtype.type)@Retention(Retentionpolicy.runtime)@Documented@Inherited@AutoConfigurationPackage@Import(Enableautoconfigurationimportselector.class) Public @interfaceenableautoconfiguration {String Enabled_override_property ="Spring.boot.enableautoconfiguration";/*** Exclude specific auto-configuration classes such that they would never be applied. * @return The classes to exclude */class<?>[] exclude ()default{};/*** Exclude specific Auto-configuration class names such that they would never be * applied. * @return The class names to exclude * @since 1.3.0 */string[] Excludename ()default{};}
    2. Enableautoconfiguration registers a Enableautoconfigurationimportselector object for the container. It is the core class of automatic configuration.

    3. Enableautoconfigurationimportselector implements the Selectimports method of Importselector:

      1. SpringFactoriesLoader.loadFactoryNames()Gets the configuration-related metadata.

        1234567891011
        # Auto Configureorg.springframework.boot.autoconfigure.enableautoconfiguration=\ Org.springframework.boot.autoconfigure.admin.springapplicationadminjmxautoconfiguration,\ Org.springframework.boot.autoconfigure.aop.aopautoconfiguration,\ Org.springframework.boot.autoconfigure.amqp.rabbitautoconfiguration,\ Org.springframework.boot.autoconfigure.batch.batchautoconfiguration,\ Org.springframework.boot.autoconfigure.cache.cacheautoconfiguration,\ Org.springframework.boot.autoconfigure.cassandra.cassandraautoconfiguration,\ Org.springframework.boot.autoconfigure.cloud.cloudautoconfiguration,\ ... Org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration

      2. necessary handling. such as de-heavy, sort, conditional dependent.

      3. Returns the associated instance object registered with the container.

    4. Container initialization is complete. (Additional services such as Commandlinerunner are provided.)

External configuration

The Springboot combines the power of spring with great extensibility. There are a large number of starter modules in Springboot and its surrounding projects to achieve various functions. such as Spring-boot-starter-web, Spring-boot-starter-jmx,spring-boot-starter-mybatis. They have their own default configuration, or they can be customized with spring's properties API.

Property API
  • PropertySource: Property source, Key-value property to abstract, e.g. for configuration data
  • PropertyResolver: Property parser, used to parse the value of the corresponding key
  • Environment: The environment itself is a propertyresolver, but provides the Profile characteristics that can be obtained according to the environment data (that is, to activate different profiles, you can get different attribute data, such as the configuration for the multi-environment scenario (formal machine, test machine, Development Machine DataSource configuration))
  • Profile: section, only the component/configuration of the active section is registered to the spring container, similar to the profile in Maven
Configure reading order

SpringBootThe configuration is configured and overwritten in a special order. Allows developers to configure services, static files, environment variables, command line parameters, annotation classes, etc., to be used for external configuration. The overwrite order is as follows:

    1. Command-line arguments
    2. JNDI Properties from Java:comp/env
    3. Operating system Environment variables
    4. A configuration file outside the packaged jar (including the propertis,yaml,profile variable.)
    5. The configuration file within the packaged jar (including the propertis,yaml,profile variable.)
    6. @propertysource Annotations for @Configuration classes
    7. Apply Framework default configuration

The application.properties and application.yml files can be placed in the following four locations. Also has an overlay order (high-low)

    • External, in the/config subdirectory relative to the application running directory.
    • External, in the directory where the application is running.
    • Built-in, within config package.
    • Built-in, in the Classpath root directory.

Each module has its own configuration. Don't dwell on it here.

Actuator

Actuator endpoint for the application of internal information monitoring and inspection. The actual development and production (necessary control) scenario, its role is essential.

(in addition to providing a rest service. Remote shell, and Mbean mode, for similar purposes).

Original creation: www.rapharino.com Author: Rapharino in, please continue to follow Rapharino's blog.

Spring Boot Concept Knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.