Spring Boot Quick Start

Source: Internet
Author: User

What is Spring boot

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. In my words, spring boot is not really a new framework, it is configured with many frameworks by default, as Maven integrates all the jar packages, and spring boot integrates all the frameworks (not knowing if the analogy is appropriate).

What are the benefits of using spring boot?

In fact is simple, fast, convenient! What do we need to do when we need to build a spring Web project?

1) Configure Web. XML to load spring and spring MVC

2) Configure the database connection, configure the spring transaction

3) Configure the load profile to read, open annotations

4) Configuration log file

...

Deploy Tomcat debugging after configuration is complete

...

Now very popular microservices if I just need to send a message to this project if my project is just to produce one point; I need to toss it all over again!

But what if I use Spring boot?

Very simple, I just need very few configuration can quickly and easily build up a set of Web projects or build a micro-service!

Quick Start

MAVEN Build Project

1. Visit http://start.spring.io/

2. Select the Build tool Maven project, Spring boot version 1.3.6, and some engineering basics, click "Switch to the full version." Java version Selection 1.7, see the following:

3. Click Generate Project to download the item Compression pack

4, after decompression, using Eclipse,import---Existing Maven Projects-----and then select the extracted folder, Finsh,ok done!

Project Structure Introduction

As shown, Spring boot has a total of three infrastructure files:

L Src/main/java Program Development and main program entrance

L src/main/resources configuration file

L Src/test/java Test procedure

In addition, the Spingboot recommended directory results are as follows:

Root Package Structure: Com.example.myproject

COM  +-Example    + myproject      +-Application.java      |      +-Domain      |  +-Customer.java      |  +-Customerrepository.java      |      +-Service      |  +-Customerservice.java      |      +-Controller      |  +-Customercontroller.java      |

1, Application.java recommended to follow the directory, mainly used to do some framework configuration

2. The domain directory is primarily used for entity and data Access Layer (Repository)

3, service layer is mainly business class code

4, controller responsible for page access control

Using the default configuration can save a lot of configuration, of course, you can also make changes according to your liking

Finally, start the application Main method, where a Java project is built!

Introducing the Web Module

1. Add Web-enabled modules in Pom.xml:

<dependency>        <groupId>org.springframework.boot</groupId>        <artifactId> Spring-boot-starter-web</artifactid> </dependency>

There are two modules in the Pom.xml file by default:

Spring-boot-starter: core modules, including automatic configuration support, logging and YAML;

Spring-boot-starter-test: Test modules, including JUnit, Hamcrest, Mockito.

2. Write Controller content

@RestControllerpublic class Helloworldcontroller {    @RequestMapping ("/hello") public    String Index () {        Return "Hello World";}    }

@RestController means that the controller inside the method is output in JSON format, no longer write what Jackjson configuration!

3, start the main program, open the browser access Http://localhost:8080/hello, you can see the effect, there is a very simple wood!

How to do unit tests

Open the test portal under src/test/, write a simple HTTP request to test, use MOCKMVC, and use Mockmvcresulthandlers.print () to print out the execution results.

@RunWith (Springjunit4classrunner.class) @SpringApplicationConfiguration (classes = mockservletcontext.class) @ Webappconfigurationpublic class Helloworldcontrolertests {    private MOCKMVC mvc;    @Before public    void SetUp () throws Exception {        MVC = Mockmvcbuilders.standalonesetup (New Helloworldcontroller ( ). Build ();    }    @Test public    void Gethello () throws Exception {    mvc.perform (mockmvcrequestbuilders.get ("/hello"). Accept ( Mediatype.application_json)                . Andexpect (Mockmvcresultmatchers.status (). IsOk ()).                Anddo ( Mockmvcresulthandlers.print ())                . Andreturn ();    }}

Debugging of the development environment

Hot start-up is already common in normal development projects, although in the course of developing Web projects, changes in project restart are always error-springboot, but the debugging support is very good, after the modification can take effect immediately, you need to add the following configuration:

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<optional>true</optional>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration>

<fork>true</fork>

</configuration>

</plugin>

</plugins>

</build>

This module is disabled when it is running in a fully packaged environment. If you start an app with Java-jar or start with a specific classloader, it will think of it as a "production environment."

Summarize

Using spring boot makes it easy and quick to build projects so that we don't care about the compatibility between frameworks, the right version and so on, we want to use anything, just add a configuration, so using sping boot is a great fit to build microservices.

Demo Demo Address: Https://github.com/ityouknow/spring-boot-examples

Spring Boot Quick Start

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.