Spring Boot Introduction and Getting Started (version 2.1.6)

Spring Boot 2.1.6 版

img

Times every programmer should have a sense of when a Java programmer in contemporary step all over you on the line that I can think of to learn more

what. Impressive is the back end of the frame is stable, they are able to maintain a longer period of time in the application, without having to worry about upgrading technology. But similar

SSH, SSM these frameworks for too long, people can not wait to use a more elegant and simpler framework in place, and use it SpringBoot

Students.

Spring Boot Spring framework is a new family, which is used to simplify the creation and development of Spring applications. Using Spring

Boot can be very easily and quickly build applications based on the Spring Framework, which allows coding becomes simple, simple configuration change, change is simple to deploy, monitor

Simplified. Because Spring Boot can simplify, lets developers become extremely quick, so much attention in the industry. Currently in China Spring Boot

Following the trend has become more than Spring.

Learning conditions

SpringBoot frame Spring Framework is an application based on the frame, and performs the optimization into the frame Spring Framework upgrading

For new framework. Both SpringBoot Spring Framework is built on top of the frame, so I want to learn so you are willing to SpringBoot-

Given to master the Spring Framework.

Introduction

Spring Boot Overview

Spring framework is powerful, but even a very simple project, we have to configure a lot of things. So there is a Spring Boot box

Rack, its role is simply to help us automatically configured. Spring Boot core framework is automatically configured, as long as there is a corresponding jar package,

Spring to help us automatically configured. If the default configuration does not meet the demand, we can also replace the automatic configuration class, using our own configuration.

Spring Boot Spring is not enhanced, but a quick using the Spring framework for development. == Spring Boot is designed to help developers quickly ==

Spring Framework structures == == speed

img

Use Spring Boot can easily create a separate run (run jar, embedded servlet container), entry-level production-based Spring Framework

Currently, we use Spring Boot can not even require little Spring configuration only. In addition, Spring Boot also integrates an embedded Web services

, System monitoring and many other useful features, allowing us to quickly build enterprise-class applications.

img

== micro service is the future trend, Spring Boot project is the cornerstone of the development of micro-services. ==

1561776361365

These views is why we learn the key points SpringBoot.

Spring Boot core functionality

Spring run independently of the project

Spring Boot jar can be run directly in packet form, such as java -jar xxx.jar

Advantages are: save server resources

Embedded Tomcat and Jetty container

Spring Boot can select the embedded Tomcat, Jetty, so we do not need to deploy the project package in the form of war.

A starter POMs to simplify the configuration Maven

E.g. automatically introduced into the incorporated spring-boot-starter-web

1562656700215

spring-boot-starter-parent the Start is a special, which is used to provide relevant Maven dependency, after use it commonly used by package

Lai can save version label.

Automatic Configuration Spring

Spring Boot will be based in the classpath jar package, class, automatically configured to jar Bean bag category, this will greatly reduce our distribution to be used

Home. Of course SpringBoot only considered the most development scenarios, not all scenarios, we need to automatically configure if the actual development

Bean, while Spring Boot is not met, you can customize the automatic configuration.

Quasi-production application monitoring

Spring Boot-based http, ssh, telnet project run-time monitoring.

Xml configuration and no code generation

Spring Boot extensive use of annotation to provide new features spring4.x-free code generation and xml configuration. spring4.x promote the use of Java with

置和注解配置组合,而Spring Boot不需要任何xml配置即可实现spring的所有配置。

​ 综上我们可以得出:Spring Boot让开发变得简单,让部署变得简单,让监控变得简单,让配置变得更简单。

快速入门

​ Spring Boot 2.1.6.RELEASE需要Java 8,并且与Java 11兼容(包括在内)。

​ 还需要Spring Framework 5.1.8.RELEASE或更高版本。

第一个 Spring Boot 项目

创建项目

1561777338931

1561800412596

1561777451958

1561800452777

编写 pom.xml

​ 根据官网 Spring Boot 文档提示从 spring-boot-starter-parent 项目继承并向一个或多个 Starters

明依赖项。Spring starter 是 Spring Boot 的核心,可以理解为一个可拔插式的插件,例如,你想使用Reids插

件,那么可以使用 spring-boot-starter-redis;如果想使用MongoDB,可以使用 spring-boot-starter-data-

mongodb。

<!-- 引入springboot父类依赖 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
</parent>

<dependencies>
    <!-- springboot-web 组件 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

HelloWorld

​ HelloWorldController.java

package com.springboot.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

// 表示该服务接口全部返回 json 格式 默认每个方法都加了 @ResponseBody
@RestController
// 表示所扫描到的服务
@EnableAutoConfiguration
public class HelloWorldController {

    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    @RequestMapping("/getMap")
    public Map<String, Object> getMap() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", "200");
        resultMap.put("msg", "成功");
        return resultMap;
    }

    // 主函数运行 springboot 项目
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldController.class, args);
    }

}

​ 结果

......

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.10.RELEASE)

......

...Registering beans for JMX exposure on startup
...Tomcat started on port(s): 8080 (http)
...Started HelloWorldController in 5.806 seconds (JVM running for 10.606)

Spring Boot 第二种启动方式

​ 项目中肯定不止一个 Controller,我们当然希望项目启动时将所有的 Controller 全部加载进来,这种情况又

该如何配置呢?比如此时有两个 Controller,都需要被执行。

​ HelloWorldController.java

package com.springboot.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloWorldController {

    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    @RequestMapping("/getMap")
    public Map<String, Object> getMap() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", "200");
        resultMap.put("msg", "成功");
        return resultMap;
    }

}

​ IndexController.java

package com.springboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

}

​ App.java

package com.springboot.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

// 扫描指定包下的服务
@ComponentScan("com.springboot.controller")
// 表示所扫描到的服务
@EnableAutoConfiguration
public class App {

    // 项目中不需要有多个 main 方法只需要有一个入口就可以了
    public static void main(String[] args) {
        // 主函数运行 springboot 项目
        SpringApplication.run(App.class, args);
    }

}

​ 删除其他服务的主函数 main(),只保留一个即可。

​ 并且删除其他服务的 @EnableAutoConfiguration 注解,只在主函数服务中添加。

​ 通过 @ComponentScan 注解扫描你需要的服务。

​ 启动 App.java,其他两个服务也可以访问到了。

Spring Boot 第三种启动方式

​ 开发中,我们经常使用以下3个注解注解 main 类。

Respectively @Configuration, @EnableAutoConfiguration, @ComponentScan. Because these notes are generally a

Since use, Spring Boot will provide a unified annotation @SpringBootApplication.

Note that: @SpringBootApplicationonly scan at the same level package.

Under different packages can be used @SpringBootApplicationin combination @EnableAutoConfiguration,

@ComponentScanScanning injection.

Wherein the @ComponentScanuseful scanning Beans can specify certain annotations comprising the packets through the annotation:

@Component, @Service, @Repository, @Controller, @EntityEtc. are automatically registered as Spring Beans.

​ App.java

package com.springboot.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

// 相当于 @ComponentScan("com.springboot.app") 只扫描同级包下
// 解决办法提升当前App类的包范围("com.springboot"),或者结合 @ComponentScan使用
@SpringBootApplication
// 扫描指定包下的服务
@ComponentScan("com.springboot.controller")
public class App {

    // 项目中不需要有多个 main 方法只需要有一个入口就可以了
    public static void main(String[] args) {
        // 主函数运行 springboot 项目
        SpringApplication.run(App.class, args);
    }

}

Guess you like

Origin www.cnblogs.com/5Dylan/p/springboot-abc.html