Spring Boot Project Setup

Source: Internet
Author: User

1.Spring Boot Overview

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. It is well known that the spring platform has been criticized for a large number of XML configurations and complex dependency management, and the advent of spring boot is to simplify operations. Compared with the traditional spring, the project construction is simpler, more convenient and faster.

2. Project Construction

This article uses idea to build the spring Boot,demo structure diagram as follows:

It is convenient to generate the spring boot project via idea, and the steps are no longer detailed, so you can refer to other information on the web, such as the main generation:

      • Src/main/java program Development and main program entry
      • Src/main/resources configuration file
      • Src/test/java Test procedure

The default Pom.xml build jar package dependencies are as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "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.snail</groupId>    <Artifactid>Bootdemo</Artifactid>    <version>0.0.1-snapshot</version>    <Packaging>Jar</Packaging>    <name>Bootdemo</name>    <Description>Demo Project for Spring Boot</Description>    <Parent>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-parent</Artifactid>        <version>1.5.4.RELEASE</version>        <RelativePath/> <!--Lookup parent from repository -    </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>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-web</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-test</Artifactid>            <Scope>Test</Scope>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-configuration-processor</Artifactid>            <Optional>True</Optional>        </Dependency>    </Dependencies>

<Build> <Plugins> <plugin> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-maven-plugin</Artifactid> </plugin> </Plugins> </Build></Project>

3. Test Cases

Demo does not configure the database, just simply test the HTTP request, and combined with real-world scenarios, our configuration file is often in the online environment, the production environment configuration is not the same, can be configured to obtain the corresponding file, as shown below.

(1) Entity class

Where the purpose of the configurationproperties annotation is to map the configuration file, a new configuration item application.yml, APPLICATION-PRODUCT.YML (production environment) is created in the configuration file Yml file. APPLICATION-TEST.YML (test environment), the corresponding mappings in the configuration file can be obtained by prefix customerinfo.

/*** Customer basic information * Create by Snailtech * 2017-07-24 16:31 **/@Component @configurationproperties (prefix= "CustomerInfo") Public classCustomerInfo {/*** Name*/    PrivateString name; /*** Mobile phone number*/    PrivateString Mobile; /*** Age*/    PrivateInteger age;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getmobile () {returnMobile; }     Public voidSetmobile (String mobile) { This. Mobile =Mobile; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }}

(2) Application.yml

Spring:  Profiles:    active:product

With this configuration, active is the product that reads the application-product.yml file, where port is the port number of the Web container (which is tomcat by default), and Context-path is the virtual path.

(3) Application-product.yml

Server:  port:8080  context-path:/bootdemotesturl:http://www.baidu.comcustomerinfo:  name:snail  mobile:18818718711 (production environment)  age:30

(4) Application-test.yml

Server:  port:8080  context-path:/bootdemotesturl:http://www.baidu.comcustomerinfo:  name:snail  mobile:18818718711 (test environment)  age:30

(5) Configuring the controller layer

@RestController equivalent to @[email protected], which is returned in JSON format, @Value ("${testurl}") is used to obtain the Testurl configuration item in the configuration file, @ The Requestparam is used to receive request parameters, and the rest of the writing is no different from SPRINGMVC's notation.

 PackageCom.snail;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.web.bind.annotation.*;ImportJavax.annotation.Resource;/*** Create by Snailtech * 2017-07-24 16:10 **/@RestController @requestmapping (value= "/test") Public classTestController {@Value ("${testurl}")    PrivateString Testurl; @AutowiredPrivateCustomerInfo CustomerInfo; //@RequestMapping (value= "/hello", method = Requestmethod.get)@GetMapping (value = "/hello")     PublicString Hello (@RequestParam ("id") (Integer xx) {returnCustomerinfo.getmobile () +xx; }}

(6) Compiling

Compiling is very simple, built-in Tomcat, no need to manually configure Tomcat in the SSM project as long as you run the program.

(7) Operation

Spring Boot Project Setup

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.