1. 程式人生 > 實用技巧 >Spring boot 環境搭建

Spring boot 環境搭建

https://www.jianshu.com/p/ffe5ebe17c3a Spring 和Springboot的區別: 1)Spring框架為開發Java應用程式提供了全面的基礎架構支援。它包含一些很好的功能,如依賴注入和開箱即用的模組,如:
Spring JDBC 、Spring MVC 、Spring Security、 Spring AOP 、Spring ORM 、Spring Test 2)Spring Boot基本上是Spring框架的擴充套件,它消除了設定Spring應用程式所需的XML配置,為更快,更高效的開發生態系統鋪平了道路
Spring Boot中的一些特點:

1:建立獨立的spring應用。
2:嵌入Tomcat, Jetty Undertow

而且不需要部署他們。
3:提供的“starters” poms來簡化Maven配置
4:儘可能自動配置spring應用。
5:提供生產指標,健壯檢查和外部化配置
6:絕對沒有程式碼生成和XML配置要求


Spring 1)依賴注入 2)控制反轉 AOP面向切面程式設計 Springboot Spring->servlet->web應用程式 servlet 1)tomcat 2)undertom 3)jetty 如何搭建SpringBoot專案 Mevan依賴 mevan倉庫:https://mvnrepository.com/ 一:引入必要的依賴 1) Sping Boot Starter Parent
點選,copy如圖部分,到pom檔案中

pom檔案,import changes

spring-boot-starter-parent的主要作用

  • 指定編碼格式預設使用utf-8。
  • 指定java版本預設使用1.8。
  • 提供Dependency Management進行專案依賴的版本管理,如指定實際依賴的版本號(已經做版本相容性測試)。
  • 預設的資源過濾和外掛管理。

2)Spring Boot Web Starter

Spring Boot Web Starter

web的場景,自動幫我們引入了web模組開發需要的相關jar包

備註:集成了tomcat

注意要與上面版本是一致的

點選copy,如圖紅框位置到pom檔案中

pom 檔案,需要import changes,需要一些時間...

二、建包及java檔案

三、使用Spring 託管

@SpringBootApplication
public class TappApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(TappApplication.class);

application.run(args);

} }

四、new 子包,建立controller和service

通過Bean來管理類

Spring->Bean Service ->Bean Controller->Bean Repository ->Bean

controller->LoginController.java

@RestController * 注意是@RestController  而不用Controller  否則會識別為mvc模式,而報錯  
public class LoginController {
private LoginService loginService;

@Autowired
public LoginController(LoginService loginService) {
this.loginService = loginService;
}

@GetMapping ("/login")
public String doLogin(
@RequestParam("u") String userName,
@RequestParam("p") String password
) {
System.out.println(userName + password);
if (loginService.checkLogin(userName, password) == true) {
return "login success";
}
else {
return "login fail";
}

// return "hello world";

}

}

配置埠為8080

新增配置檔案resource ->application.properties

server.port = 8080


service.LoginService.java 檔案
@service
public class LoginService {
    public boolean checkLogin(String userName,String password){
        if(userName.equals("123")&& password.equals("456")){
            return true;
        }
        else
            return false;

    }
}

run main

訪問地址:

注入有三種形式:1)按型別 2)按名字 3)人工

http 請求 基於tcp協議的協議 restful 後端api風格 1)get (獲取資源) 2)post(更新資源) 3)put(新增)4)delete(刪除資源)