Springboot的第一個程序
一、什麼是Springboot
隨着動態語言的流行(Ruby、Groovy、Scala、Node.js),Java的開發顯得格外的笨重:繁多的配置、低下的開發效率、複雜的部署流程以及第三方技術集成難度大。在上述環境下,Springboot應運而生。它使用"習慣優於配置"(項目中存在大量的配置,此外還內置一個習慣性的配置,讓你無須手動進行配置)的理念讓你的項目快速運行起來。使用springboot很容易創建一個獨立運行(運行jar,內嵌servlet容器)、準生產級別的基於Spring框架的項目,使用springboot你可以不用或者只需要很少的Spring配置。
二、Springbot的優缺點
優點:
1.快速構建項目;
2.對主流開發框架的無配置集成;
3.項目可獨立運行,無須外部依賴servlet容器;
4.提供運行時的應用監控;
5.極大地提高了開發、部署效率;
6.與雲計算的天然集成。
缺點:
1.書籍文檔較少且不夠深入;
2.如果你不認同Spring框架,這也許是它的缺點,但建議你一定要使用Spring框架。
三、Springboot入門程序
(1)設置spring boot的parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
說明:Spring boot的項目必須要將parent設置爲spring boot的parent,該parent包含了大量默認的配置,大大簡化了我們的開發。
(2)導入spring boot的web支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
(3)添加Spring boot的插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
即pom.xml:
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<groupId>com.hcx.springboot</groupId>
<artifactId>hcx-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- 連接池 -->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-spring</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 資源文件拷貝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
(4)編寫第一個Spring Boot的應用
package com.hcx.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@SpringBootApplication //聲明這是一個springboot的應用
@Configuration //聲明該類爲一個配置類
public class HelloApplication {
//springboot的項目命名中,一般都有一個xxxApplication類,該類作爲springboot項目的入口類
@RequestMapping("hello")
@ResponseBody
public String hello(){
return "hello world";
}
public static void main(String[] args) {
/**
* 第一個參數:要運行的應用(該應用一定要包含@SpringBootApplication註解)
* 第二個參數:直接用args,也可以指定內置參數
*/
SpringApplication.run(HelloApplication.class, args);
}
}
代碼說明:
1、@SpringBootApplication:Spring Boot項目的核心註解,主要目的是開啓自動配置;
2、@Configuration:這是一個配置Spring的配置類;
3、@Controller:標明這是一個SpringMVC的Controller控制器;
4、main方法:在main方法中啓動一個應用,即:這個應用的入口;
(5)啓動應用
在Spring Boot項目中,啓動的方式有兩種,一種是直接run Java Application另外一種是通過Spring Boot的Maven插件運行。
第一種:
第二種:
第二種啓動方式.png
啓動效果:
看到如下信息就說明啓動成功了:
INFO 6188 --- [ main] c.i.springboot.demo.HelloApplication : Started HelloApplication in 3.281 seconds (JVM running for 3.601)
(6)測試
打開瀏覽器,輸入地址:127.0.0.1:8080/hello
啓動.png
效果圖.png