Micro Services SpringBoot summary

What is SpringBoot
SpringBoot is a sub-project Spring projects, products, and we know Spring-framework belong to the spring's
official description:
Spring the Boot Makes IT the Easy to the Create Stand-alone, Production's-Grade Spring based Applications that you CAN " just run ". we take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
we usually called the Spring Boot scaffolding program. Its main role is to help us quickly build a huge spring projects, and minimize all xml configuration, so out of the box, quickly get started, let's focus on the business rather than configuration.

Why learn SpringBoot
the Java has been criticized point is bloated, cumbersome. When we are still hard to build the project, the feature may Python programmers have written, the main reason is two things:

Complex configuration
items in various configurations is actually loss during development, because thinking about Spring features and solutions need to be configured to switch between thinking business problems, so write configuration crowding out the time to write application logic.
Confusion dependency management
dependency management project is a thankless thing. Decided to use the project in which the library has enough of a headache, you have to know which versions of these libraries and other library does not have a conflict, and this is a tricky question. And, dependency management is also a loss, adding a dependency not write application code. Once dependent wrong version, followed by incompatibility issues will no doubt be a productivity killer.
And SpringBoot make it to the past!

SpringBoot features
Boot Spring is the main features:

Create a separate spring applications
directly embedded tomcat, jetty and undertow (does not need to be packaged into war package deployment)
provides immobilized "starter" configuration to simplify the build configuration
automatically configured as the spring and third-party libraries
offer product-level It features such as: safety indicators, and monitoring the health of the external configuration, etc.
will not generate code, and does not require XML configuration
summary, Spring Boot for all Spring developers to provide a out of the box, very fast, wide Getting accepted to experience
more details, you can go to the official website to view.

SpringBoot Quick Start
Mode 1: Maven project to build
1, access http://start.spring.io/
shown below 2, choose to build tools Maven Project, Java, Spring Boot version 2.1.3 as well as some basic project information, refer to the map :

3. Click Generate Project Download the project archive
4, unpacked, use Idea import projects, File -> New -> Model from Existing Source ... -> select the unzipped file folder -> OK, select Maven all the way Next, OK done!
5. If you are using Eclipse, Import -> Existing Maven Projects -> Next -> select the file after decompression folder -> Finsh, OK done!

Second way: Idea building project
1, environmental requirements:

2, Idea create an empty demo project, and then create moduel in the project (maven skip skeleton)
3, introducing dependence: How SpringBoot help us manage the dependencies?
SpringBoot offers a project called spring-boot-starter-parent's, which has been dependent on a variety of common (but not all) of version management, our project need to be the parent of this project works, so we do not worry about dependence the version of the problem, and what needs to rely on, coordinates can be directly introduced

Add the parent project
<parent>
<groupId> org.springframework.boot </ groupId>
<artifactId> the Spring-the Boot-Starter-parent </ artifactId>
<Version> 2.0.2.RELEASE </ Version>
</ parent>
Add Start an
order for SpringBoot help us complete a variety of automatic configuration, we must introduce automatic configuration dependent SpringBoot provided, we called a starter. spring-boot-starter-parent project will be declared as a dependency or more starters, we can introduce the appropriate starter according to project requirements, since we are web project, where we introduce web starters:
<the Dependencies>
<dependency>
<groupId> org.springframework.boot </ groupId>
<artifactId> the Spring-the Boot-Starter-Web </ artifactId>
</ dependency>
</ the dependencies>
this time, we will find out a lot more than the project dependencies, these SpringBoot are based on spring-boot-starter-web this dependency automatically introduced, and all versions have been good management, not conflict.

完整的POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.ityang.springboot</groupId>
<artifactId>ityang-springboot</artifactId>
<version>1.0.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>

<Dependencies>
<dependency>
<the groupId> org.springframework.boot </ the groupId>
<the artifactId> Starter-Spring-Boot-Web </ the artifactId>
</ dependency>
</ Dependencies>
</ Project>

. 4, add the global start class: If a separate written inside a Controller, Controller can not start more than one, each of the main methods are listening on port 8080. So do some framework to write a separate configuration

@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

5、编写HelloController

@RestController
public class HelloController {
@GetMapping("show")
public String test(){
return "hello Spring Boot!";
}
}

6、访问localhost:8080/show,测试

spingboot suggested directory results are as follows (root package structure: com.example.myproject)

COM
+ - Example
+ - myproject
+ - Application.java
|
+ - Domain
| + - Customer.java
| + - CustomerRepository.java
|
+ - Service
| + - CustomerService.java
|
+ - the Controller
| + - CustomerController.java
|
comment analysis
1, @ EnableAutoConfiguration
notes Description: turn on the automatic configuration spring applications, SpringBoot dependent and based on your own definition of bean you added, trying to guess what you want and configuration. For example, we introduced the spring-boot-starter-web, and this launcher to help us add a tomcat, SpringMVC dependence. At this point you know you are automatically configured to develop a web application, so to help you complete the default configuration of the web and SpringMVC!

2, @ ComponentScan
explanatory notes: assembly instructions configured scan. Providing similar <context: component-scan> The tag to specify the packet to be scanned by basePackageClasses or basePackages properties. If you do not specify these attributes, then this package will declare the class notes where to start, scan package and the child package.
Our @ComponentScan class notes class function declaration is to start where the main, the scan package is the class where the package and its subpackages. General class will start in a relatively shallow package directory.

3, @ SpringBootApplication
it is actually a combination comment to focus on here are three:

@SpringBootConfiguration
@EnableAutoConfiguration: turn on the automatic configuration
@ComponentScan: Open comments Scan
4, @ SpringBootConfiguration
by viewing annotated source, found in the notes on it, and a @Configuration comment. By reading the above comments we know: the role of this comment is to declare the current class is a class configuration, then Spring will automatically scan @Configuration to add classes, and reads the configuration information in it. The @SpringBootConfiguration is to declare the current class is the class SpringBoot configuration application, the project can be only one. So in general we do not need to add their own.

Guess you like

Origin www.cnblogs.com/itboxue/p/12387398.html