Spring Boot Starter Basics (i)

Source: Internet
Author: User
Spring Boot Starter Basics (i)

Original address: Spring Boot Starter Basics (i)
Blog address: http://www.extlight.com First, preface

Spring Boot is a new framework provided by the Pivotal team designed to simplify the initial setup and development of new Spring applications. The framework uses a specific approach to configuration, which eliminates the need for developers to define boilerplate configurations.

This series is based on Quick start, can be used as a tool for a small manual reading second, environment construction

To create a MAVEN project, the directory structure is as follows:

2.1 Adding Dependencies

To create a MAVEN project, add the following dependencies in the Pom.xml file:

<!--define public resource versions--<parent> <groupId>org.springframework.boot</groupId> <artifactid>s 
pring-boot-starter-parent</artifactid> <version>1.5.6.RELEASE</version> <relativepath/> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding > <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version> 1.8</java.version> </properties> <dependencies> <!--The parent is introduced, so there is no need to specify the version--<!-- Contains jar resources such as MVC,AOP-<dependency> <groupId>org.springframework.boot</groupId> &L t;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> 
2.2 Creating directories and configuration Files

Create a directory of Src/main/resources source files and create folders for application.properties files, static, and templates in this directory.

Application.properties: Used to configure the configuration data required for the project to run.

Static: Used for the storage of resources, such as: CSS, JS, pictures and so on.

Templates: Used to store template files.

The directory structure is as follows:

2.3 Creating a startup class

Under the Com.light.springboot package, create the startup class as follows:

/**
 The annotation specifies that the item is Springboot, which is
 automatically assembled as a program portal by the Web-dependent environment
 
**/@SpringBootApplication public
class springbootapplication {public

    static void Main (string[] args) {
        Springapplication.run ( Springbootapplication.class, args);
    }
}
2.4 Case Demo

Create the Com.light.springboot.controller package and create a controller class under the package, as follows:

@RestController public
class TestController {

    @GetMapping ("/helloworld") public
    String HelloWorld () {
        return "HelloWorld";
    }
}

In the Springbootapplication file, right-click Run as, Java application. When you see the word "Tomcat started on port (s): 8080 (http)", the description starts successfully.

Open the browser to access Http://localhost:8080/helloworld and the results are as follows:

The reader can use the STS development tool, which integrates plug-ins to create the Spingboot project directly, which automatically generates the necessary directory structure. third, hot deployment

When we modify the file and create the file, we need to restart the project. This frequent operation is a waste of time, and configuring a hot deployment allows the project to automatically load the changed files, eliminating the manual action.

Add the following configuration in the Pom.xml file:

<!--hot Deployment-
<dependency>
    <groupId>org.springframework.boot</groupId>
    < artifactid>spring-boot-devtools</artifactid>
    <optional>true</optional>
    <scope >true</scope>
</dependency>
<build>
    <plugins>
        <plugin>
            <groupid>org.springframework.boot</groupid >
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- Without this configuration, Devtools does not take effect-
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

After configuring the Pom.xml file, we launch the project, randomly create/modify a file and save it, we will find the console print Springboot reload the file information.

The demo diagram is as follows:

four, multi-environment switch

Application.properties is the configuration information required for Springboot to run.

When we are in the development phase, we use our own machine to develop, test the test server testing, and use the official environment when the server is on line.

These three environments require different configuration information, and when we switch the environment to run the project, we need to manually modify the configuration information, which is very error-prone.

To address these issues, Springboot provides a multi-environment configuration mechanism that makes it very easy for developers to switch between different configuration environments as needed.

Create three configuration files under the Src/main/resources directory:

Application-dev.properties: for development environment
Application-test.properties: for test environments
Application-prod.properties: For production environments

We can set different information in this three configuration file, application.properties Configure the public information.

Configure in Application.properties:

Spring.profiles.active=dev

Indicates that the Application-dev.properties file configuration is activated, and springboot loads information that uses application.properties and application-dev.properties configuration files.

Similarly, the value of spring.profiles.active can be modified to test or PROD to achieve the purpose of switching the environment.

The demo diagram is as follows:

v. Configuration Log 5.1 configuration Logback (recommended for official use) 5.1.1 Configuration log file

Spring boot will load Classpath:logback-spring.xml or Classpath:logback-spring.groovy by default.

If you need a custom file name, configure the Logging.config option in Application.properties.

Under Src/main/resources, create a logback-spring.xml file that reads as follows:

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.