Spring官網教程學習1和使用Gradle


spring官網
1、工程構建
我選擇的方式是IDEA+gradle

構建工程

IDEA構建Gradle工程

Gradle進行Spring配置

知識和概念

gradle命令

gradle:開始構建
gradle tasks :查看有哪些任務
gradle build :編譯代碼 生產jar包
gradle wrapper --gradle-version 2.13 無環境構建

build.gradle

plugins {
id 'java'--java環境 會多了javadoc-=(including tasks for building the project, creating JavaDoc, and running tests)
}
repositories {
mavenCentral() ---配置倉庫--(build should resolve its dependencies from the Maven Central repository)
}

sourceCompatibility = 1.8 --java版本
targetCompatibility = 1.8
dependencies {
compile "joda-time:joda-time:2.2" ---配置依賴
testCompile "junit:junit:4.12"
}
jar {
baseName = 'gs-gradle'---打成jar的名稱(已經過期l)
version = '0.1.0'
}

這里可以改jar名稱 否則會打倆個包

dependencies -compile

分為 providedCompile、testCompile

Gradle Wrapper

包含linux+IOS+Window ,(沒有安裝gradle環境也可以使用gradle構建)。
用途:Add it to your version control system, and everyone that clones your project can build it just the same
每一個克隆項目的人 ()有gradle Wrapper文件的都可以直接構建此項目(不用安裝gradle)
1、 gradle wrapper --gradle-version 2.13 (生成gradle構建版本文件)

生成的文件格式

2、運行./gradlew build 或者 運行gradlew.bat (gradle構建)


已經構建好了 但是至少生命了依賴而已 還不能運行
3、 use gradle’s application plugin
添加-+
apply plugin: 'application'
mainClassName = 'hello.HelloWorld'
4、./gradlew run 或者gradlew.bat run

RESTful Web Service

Building a RESTful Web Service

發送參數(設置默認差數)和設置依賴

plugins {
	id 'org.springframework.boot' version '2.3.2.RELEASE'
	id 'io.spring.dependency-management' version '1.0.8.RELEASE'
	id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
}

test {
	useJUnitPlatform()
}

返回值參數



AtomicLong 可以自動增加返回值

JOSN

uses the Jackson JSON library to automatically marshal instances of type Greeting into JSON.

Resource Controller

構建一個可執行的jar包

springboot命令

編譯運行spring服務 ./gradlew bootRun

構建 ./gradlew build
運行jar包 java -jar build/libs/gs-rest-service-0.1.0.jar

http://localhost:8080/greeting http://localhost:8080/greeting?name=User

上傳文件

MultipartConfigElement class

(which would be in web.xml)
這個MultipartConfigElement上傳的配置 springboot 已經配好了(自動配置)
As part of auto-configuring Spring MVC, Spring Boot will create a MultipartConfigElement bean and make itself ready for file uploads.

最好放在nosql文件存儲 或者數據庫

control

storageService

private final StorageService storageService;

	@Autowired
	public FileUploadController(StorageService storageService) {
		this.storageService = storageService;
	}

上傳限制

啟動時候刪除所有文件

FileSystemStorageService

文件服務

找不見文件的自定義異常

StorageFileNotFoundException

文件位置配置

改變文件路徑


結果

可以學習



獲取配置類來 初始化final對象的時候 (保證bean是唯一的 不可改變的)

繼續看官網教程
Spring Boot有四大神器,分別是auto-configuration、starters、cli、actuator

定時任務(scheduling tasks)

1、周期任務

2、開啟定時任務

springboot入門

啟動類CommandLineRunner

spring的測試

1、依賴

testImplementation('org.springframework.boot:spring-boot-starter-test') {
	exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}

2、MockMvc模擬請求測試

@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {

	@Autowired
	private MockMvc mvc;

	@Test
	public void getHello() throws Exception {
		mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
				.andExpect(status().isOk())
				.andExpect(content().string(equalTo("Greetings from Spring Boot!")));
	}
}

3、全堆棧集成測試(full-stack integration test)

@BeforeEach 在@test前執行

Groovy是一種基於JVM的敏捷開發語言,是用於Java虛擬機的一種敏捷的動態語言

actuator module(致動器模塊)(provides several such services (such as health, audits, beans, and more) )

應用系統的自省和監控的集成功能,可以對應用系統進行配置查看、相關功能統計等。

默認不能使用/actuator/shutdown 需要手動配置

Securing a Web Application

用途:登錄攔截

使用thymeleaf

放置thymeleaf的html

托管springMVC

@Configuration
public class MvcConfig implements WebMvcConfigurer {

	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/home").setViewName("home");
		registry.addViewController("/").setViewName("home");
		registry.addViewController("/hello").setViewName("hello");
		registry.addViewController("/login").setViewName("login");
	}

}

攔截未授權的用戶(prevent unauthorized users from viewing the greeting page at /hello)

spring-security依賴

implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security:spring-security-test'

WebSecurity配置

配置登錄注銷配置頁面

login登錄成功去/home 失敗去/login?error 注銷用戶/login?logout

http://localhost:8080

SpringJDBC (JdbcTemplate 關系型數據)

依賴

implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
	runtimeOnly 'com.h2database:h2'

直接命令行啟動

插入語句

First, install some DDL by using the execute method of JdbcTemplate.

Second, take a list of strings and, by using Java 8 streams, split them into firstname/lastname pairs in a Java array.

Then install some records in your newly created table by using the batchUpdate method of JdbcTemplate. The first argument to the method call is the query string. The last argument (the array of Object instances) holds the variables to be substituted into the query where the ? characters are.
首先,使用jdbctemplate的執行方法安裝一些ddl。

其次,獲取一個字符串列表,通過使用java 8 streams,在java數組中將它們分成名字/姓氏對。

然后使用jdbctemplate的batchupdate方法在新創建的表中安裝一些記錄。方法調用的第一個參數是查詢字符串。

最后一個參數(對象實例數組)保存要替換到查詢中的變量。

查詢語句(使用了lamb表達式 比較難以理解)



使用h2的數據庫 沒有數據庫配置環境


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM