【學習筆記】springboot教程(1)第一個demo
第一個demo
摘要: 先了解下springboot到底是個什麼東西,能用來幹什麼?有什麼好處?也就是為什麼要學習他用他。然後給個網上到處都能找到的demo。
前言
1、什麼是springboot?
簡單的理解就是一個快速開發框架集合,大家都叫他快速開發腳手架。
2、springboot有什麼特徵?
它遵循“習慣由於配置”原則,使用springboot只需要少量配置,我們大部分時候可以使用他的預設配置;
它可以讓我們搭建專案更快速,可無配置整合第三方框架;
完全不使用xml配置,只是用自動配置或是Java config
它內嵌了servelt容器(tomcat、jetty),應用可以用jar的方式執行
集成了應用執行狀態的監控
3、學習springboot需要什麼知識儲備?
java這個不用說了,哈哈
maven這個必須會用啦
spring 、spring MVC 這兩個是最基本的,這裡的spring 必須是 4.x版本,因為springboot使用了spring4.x 的@Conditional註解。
想要學好它你還必須對設計模式有一定理解,還有core java中的 反射、註解、泛型 這些必不可少了,這部分有助於我們去理解原始碼。
第一個springboot demo
1、 新建一個maven工程,大家可以直接到spring官網上去新建一個,下載下來,或是自己建一個。我提倡手動。
2、引入springboot依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
完整的pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>
<groupId>com.pxk</groupId>
<artifactId>springbootdemo </artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories> </project>
當然這是一個web工程,專案結構如下圖
Application.java
package com.pxk;import org.apache.log4j.Logger;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { private static Logger logger = Logger.getLogger(Application.class); /**
* Main Start
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
logger.info("============= SpringBoot Start Success =============");
}
}
HelloController1.java
package com.pxk;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController public class HelloController1 { @RequestMapping("/")
String home() {
return "Hello World pxk!";
}
@RequestMapping("/hello/{myName}")
String index(@PathVariable String myName) {
return "Hello "+myName+"!!!";
}
}
然後直接run Application,springboot就會啟動內嵌的tomcat,預設是不帶專案名,且埠為8080
訪問http://localhost:8080/ 如下圖
訪問:http://localhost:8080/hello/springboot,使用的是spring MVC的mapping方式,生成rest風格的訪問路徑