Spring Boot entry
I. Introduction
The official Spring website uses the Spring framework for development. As features and business logic become increasingly complex, applications are accompanied by a large number of XML configuration files and complex Bean dependencies.
With the release of Spring 3.0, the Spring IO team's primary key began to get rid of the XML configuration file, and used a lot of "convention over configuration" during the development process) java Config is derived from the complicated configurations in the Spring framework.
Spring Boot is a development framework abstracted in this context. It does not provide core features and extension functions of the Spring framework, it is only used for rapid and agile development of next-generation Spring-based applications. That is to say, it is not a solution to replace Spring, but a tool closely integrated with the Spring framework to improve the Spring developer experience. At the same time, it integrates a large number of common third-party library configurations (such as Jackson, JDBC, Mongo, Redis, Mail, and so on ), in Spring Boot applications, these third-party libraries can be used out-of-the-box without configuration. Most Spring Boot applications only need a small amount of configuration code, developers can focus more on business logic.
This project aims to help developers create Spring-based applications and services more easily, so that existing and new Spring developers can obtain the required Spring functions most quickly.
Spring Boot does not generate code and does not require XML configuration at all. Its main objectives are as follows:
-Provides a faster and broader entry-level experience for all Spring development work.
-Out-of-the-box, you can modify the default value to quickly meet your project needs.
-Provides common non-functional features in a series of large projects, such as embedded servers, security, metrics, health check, and external configuration.
The blogger is preparing to write a set of Spring-Boot entry posts, so that they can learn how to use Spring-Boot as soon as possible, content reference official website and online information (http://projects.spring.io/spring-boot ).
The following is the directory:
Spring-Boot entry Spring-Boot ControllerSpring-Boot ServletSpring-Boot filter and listener Spring-Boot interceptor Spring-Boot static resource processing Spring-Boot loading data Spring-Boot logging Spring-Boot connect to the database-JDBC (writing ...) Spring-Boot Connection database-JPA (writing ...) Spring-Boot Connection database-MyBatis (writing ...) Spring-Boot connects to the database-multiple data sources and transactions (writing ...) Spring-Boot unit testing (writing ...) Spring-Boot packaging and deployment (writing ...) Spring-Boot Server configuration (writing ...) Spring-Boot Web HTTPS (writing ...) Spring-Boot application monitoring (writing ...)
Development Tool: Spring Tool Suite v_3.7.2 (STS)
Address: http://spring.io/tools/sts
Ii. Getting Started instance-HelloWorld
File> New> Spring Starter Project
Next> Finish
Project created:
It can be seen that the project source code is a Java class and there is spring-boot-starter-web dependency in pom. xml.
SpringBootSampleApplication. java
package org.springboot.sample;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringBootSampleApplication { public static void main(String[] args) { SpringApplication.run(SpringBootSampleApplication.class, args); }}
Pom. xml
4.0.0
org.springboot.sample
spring-boot-sample
0.0.1-SNAPSHOT
jar
spring-boot-sample
Spring Boot Sample Web Application
org.springframework.boot
spring-boot-starter-parent
1.3.1.RELEASE
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
In this way, the project is created. Next we will create a HelloController. java to define three methods.
package org.springboot.sample.controller;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/hello")public class HelloController { @RequestMapping public String hello() { return "Hello Spring-Boot"; } @RequestMapping("/info") public Map getInfo(@RequestParam String name) { Map map = new HashMap<>(); map.put("name", name); return map; } @RequestMapping("/list") public List> getList() { List> list = new ArrayList<>(); Map map = null; for (int i = 1; i <= 5; i++) { map = new HashMap<>(); map.put("name", "Shanhy-" + i); list.add(map); } return list; }}
Now you can directly run the main method of SpringBootSampleApplication, which is the same as running a common java program.
Then we can see the built-in server container of spring-boot (Tomcat by default), which is all done for us.
The output content of the console Started SpringBootSampleApplication in 7.358 seconds (JVM running for 9.154) indicates that the service has been Started.
Enter three requests in the browser to view the results.
Http: // localhost: 8080/hello
Output: Hello Spring-Boot
Http: // localhost: 8080/hello/info? Name = shanhy
Output: {"name": "shanhy "}
Http: // localhost: 8080/hello/list
Output: [{"name": "Shanhy-1" },{ "name": "Shanhy-2" },{ "name": "Shanhy-3" },{ "name ": "Shanhy-4" },{ "name": "Shanhy-5"}]
With our Hello instance, we believe it is clear that spring-boot is so easy to create a project that you can start the service within minutes.
Spring-boot discards tedious configurations, allowing developers to focus more on the implementation of business logic. The following articles will present multiple aspects of spring-boot through instances.