軟件152 程永績
1.什么是springboot:
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。
該框架使用了特定的方式(繼承starter,約定優先於配置)來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。
Spring Boot並不是一個框架,從根本上將,它就是一些庫的集合,maven或者gradle項目導入相應依賴即可使用Spring Boot,而且無需自行管理這些庫的版本。
2.為什么要用spring boot:
Spring Boot是為簡化Spring項目配置而生,使用它使得jar依賴管理以及應用編譯和部署更為簡單。Spring Boot提供自動化配置,使用Spring Boot,你只需編寫必要的代碼和配置必須的屬性。
使用Spring Boot,只需20行左右的代碼即可生成一個基本的Spring Web應用,並且內置了tomcat,構建的fat Jar包通過Java -jar就可以直接運行。
如下特性使得Spring Boot非常契合微服務的概念,可以結合Spring Boot與Spring Cloud和Docker技術來構建微服務並部署到雲端:
一個可執行jar即為一個獨立服務
‚很容易加載到容器,每個服務可以在自己的容器(例如docker)中運行
ƒ通過一個腳本就可以實現配置與部署,很適合雲端部署,並且自動擴展也更容易
簡單而言,即Spring Boot使編碼更簡單,使配置更簡單,使部署更簡單,使監控更簡單。
3.spring boot 各模塊介紹
Spring Boot由一些模塊構成,如spring-boot, spring-boot-autoconfigure, spring-boot-starters, spring-boot-cli, spring-boot-actuator等。下面簡單介紹以下各模塊:
a. spring-boot
主庫,為其他模塊提供特性支持。包括以下內容:
SpringApplication類,提供靜態方法,方便編寫獨立運行的Spring應用。唯一的任務是創建和刷新一個合適的Spring ApplicationContext。
嵌入式web應用,自帶容器(Tomcat, Jetty等)。
b. spring-boot-autoconfigure
Spring Boot可以基於classpath下的內容配置通用應用的大部分模塊。一個@EnableAutoConfiguration注解觸發Spring上下文的自動配置。
自動配置嘗試推測用戶可能需要的bean。例如,如果H2DB在classpath中,但是用戶沒有配置任何db連接,那么spring-boot-autoconfigure推斷用戶需要一個in-memory的db,因此自動配置為用戶配置(默認創建的h2db的db名為testdb, 用戶名為sa,密碼無)。自動配置優先級低於用戶自定義的bean。
c. spring-boot-starters
starters是一系列便利的依賴描述,用戶可以增加到應用中,並由此取得Spring和相關技術的 一站式配置體驗,無需查看sample代碼並貼來貼去。例如,若用戶想要使用Spring和JPA來訪問db,則只需包含spring-boot-starter-data-jpa依賴到pom中即可。
d. spring-boot-cli
Spring的命令行應用,編譯和運行Groovy源碼,只需極少的代碼就可以運行應用,Spring CLI還可以監視文件,在它們改變時自動重新編譯和重啟。
e.spring-boot-actuator
spring boot actuator提供額外的自動配置,為你的應用裝飾一些特性,使應用在生產環境下也可以快速部署和支持。例如,若你正在編寫一個JSON web服務,該模塊會提供一個服務器,安全,日志,外部化配置,管理端點(management endpoints),評審等。關閉這些內建特性,或者擴展或替代它們都很容易。
f.spring-boot-loader
Spring Boot Loader提供秘籍允許你構建可用java –jar直接運行的jar包。一般無需直接使用spring-boot-loader,而是通過Gradle或Maven插件使用。
4.怎么使用spring boot:
maven構建項目pom代碼;
<projectxmlns="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>ygnet</groupId>
<artifactId>boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Springboot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId><scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>yg.boot.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>compile</phase>
</execution>
</executions>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.basedir}/lib</outputDirectory>
<includeScope>compile</includeScope>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
JAVA代碼:
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/test")
public class AppController {
@RequestMapping("/sayhello")
public String sayHello(){
return "Hello World!";
}
}
Application.properties代碼:
# DataSource settings
spring.datasource.url=jdbc:postgresql://localhost:5432/jcbk
spring.datasource.username=jcbk
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver
# Tomcat Server settings (ServerProperties)
server.port= 9080
server.address= 127.0.0.1
server.sessionTimeout= 30
server.contextPath= /
# Tomcat specifics
tomcat.accessLogEnabled= false
tomcat.protocolHeader= x-forwarded-proto
tomcat.remoteIpHeader= x-forwarded-for
tomcat.basedir=
tomcat.backgroundProcessorDelay=30 \# secs
Java代碼:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*/
@SpringBootApplication
public class App {
public static void main(String[] args ){
SpringApplication.run(App.class,args);
}
}
直接運行App后,結果如下圖所示。啟動后訪問http://localhost:9080/test/sayhello, 輸出 Hello World!