SpringBoot Learning Diary 1 First SpringBoot Program

Posted by seek on Tue, 14 May 2019 15:24:09 +0200

SpringBoot Learning Diary

Simple Hello Word applet

  1. Configuring pom to introduce dependencies

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  2. Writing code

    import org.springframework.boot.*;
    import org.springframework.boot.autoconfigure.*;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    @EnableAutoConfiguration
    public class Example {
        @RequestMapping("/")
        String home() {
            return "Hello World!";
        }
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Example.class, args);
        }
    }

    Finally, with these two simple configurations, you can use the browser to access localhost:8080 to the hello word page.

  3. Analysis

    @ RestController and @Enable AutoConfiguration, @RequestMapping

    • @ RestController's main function is to inform Spring that the rendering results are returned directly to the caller. == Json data==

      Amount to

      @ Controller
      @ ResponseBody
    • @ Request Mapping is the annotation of routing function in Spring MVC.
    • @ Enable AutoConfiguration Spring Boot is automatically configured through the dependency of the pom.xml file. Because Tomcat and Spring MVC are configured in Spring-boot-starter-web, the automatic configuration will be configured for Web applications.

SpringBook Hot Deployment:

<plugin>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-maven-plugin </artifactId>  
    <dependencies>   
        <!--springloaded  hot deploy -->   
        <dependency>   
            <groupId>org.springframework</groupId>   
            <artifactId>springloaded</artifactId>   
            <version>1.2.4.RELEASE</version>  
        </dependency>   
    </dependencies>   
    <executions>   
        <execution>   
            <goals>   
                <goal>repackage</goal>   
            </goals>   
            <configuration>   
                <classifier>exec</classifier>   
            </configuration>   
        </execution>   
    </executions>  
</plugin>  

Analysis of pom.xml

Dependency

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
Depending on the following:
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

Manage all SpringBoot dependencies, === SpringBoot version arbitration center==

So later import dependencies do not need to write a specific version number.

Import dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Spring-boot-starter-==web===: Dependencies needed to import Web applications.

Spring-boot-starter-x: Spring Boot's scene starter (which contains multiple integrated dependencies).

SpringBoot pulls out all the functional scenarios to make Staters just by introducing Staters

@SpringBootApplication:

Contain:

@ Component: Defined as a component that can be scanned by a bean scanner

@ ComponentScan: Scan annotated objects.

@ Enable AutoConfiguration: Automatic Configuration, Automatic Import

1 Automatic Import
    @Import(EnableAutoConfigurationImportSelector.class)

Import all subpackages in the package name of the SpringBootApplication annotated modifier class.

2 Automatic Configuration

Enable AutoConfiguration Import Selector inherits from AutoConfiguration Import Selector

@Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
            List<String> configurations = getCandidateConfigurations(annotationMetadata,
        
    }
/**
    Scan META-INF/spring.factories to get the value of EnableAutoConfiguration and wrap it into Properties. Get the fully qualified class name corresponding to EnableAutoConfiguration.class from properties and add it to the container.
*/
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
            AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
                getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
    }

SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.calss ,classLoader);

Get the values of Enable AutoConfiguration in META-INF/spring.factories under the class path, and import these values into the container as automatic configuration classes.

== Conclusion=:@SpringBootApplication is modified as the main configuration class, which scans all the components in the subpackage of the main configuration class into the Spring container, reads the configuration file from the file in the root directory, and generates the configuration class automatically.

JaveEE's overall integration and automatic configuration are in: pring-boot-autoconfigure-1.5.9.RELEASE.jar

Spring Initializer

Generate the directory of the Boot project:

  • Static: Save static files; js, css, png
  • templates: Save template pages; (jsp is not supported by default, template engine can be used)
  • Application. properties: Spring Boot configuration file, the default configuration file can be changed in this file.

configuration file

Topics: Java Spring SpringBoot xml JSON