開始
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
Features
- Create stand-alone Spring applications
- Embed Tomcat,Jetty or Undertow directory(no need to deploy WAR files)
- Automasticclly configure Spring whenever possible
- Provide production-ready features such as metrics,health checks and externalized configuration
- Absolutely no code generation and no description of all the features,plus an extensive howto for common user cases.
什么是spring boot
Spring Boot 充分利用了 JavaConfig 的配置模式以及“約定優(yōu)于配置”的理念,能夠極大的簡(jiǎn)化基于 Spring MVC 的 Web 應(yīng)用和 REST 服務(wù)開發(fā)。對(duì)于已經(jīng)熟悉 Spring 生態(tài)系統(tǒng)的開發(fā)人員來說,Spring Boot 是一個(gè)很理想的選擇。
Spring Boot提供了很多”開箱即用“的依賴模塊,都是以spring-boot-starter-xx作為命名的。下面列舉一些常用的模塊。
spring的相關(guān)模塊
Spring Boot提供了很多”開箱即用“的依賴模塊,都是以spring-boot-starter-xx作為命名的。下面列舉一些常用的模塊。
- spring-boot-starter-logging :使用 Spring Boot 默認(rèn)的日志框架 Logback。
- spring-boot-starter-log4j :添加 Log4j 的支持。
- spring-boot-starter-web :支持 Web 應(yīng)用開發(fā),包含 Tomcat 和 spring-mvc。
- spring-boot-starter-tomcat :使用 Spring Boot 默認(rèn)的 Tomcat 作為應(yīng)用服務(wù)器。
- spring-boot-starter-jetty :使用 Jetty 而不是默認(rèn)的 Tomcat 作為應(yīng)用服務(wù)器。
- spring-boot-starter-test :包含常用的測(cè)試所需的依賴,如 JUnit、Hamcrest、Mockito 和 spring-test 等。
- spring-boot-starter-aop :包含 spring-aop 和 AspectJ 來支持面向切面編程(AOP)。
- spring-boot-starter-security :包含 spring-security。
- spring-boot-starter-jdbc :支持使用 JDBC 訪問數(shù)據(jù)庫(kù)。
- spring-boot-starter-redis :支持使用 Redis。
- spring-boot-starter-data-mongodb :包含 spring-data-mongodb 來支持 MongoDB。
- spring-boot-starter-data-jpa :包含 spring-data-jpa、spring-orm 和 Hibernate 來支持 JPA。
- spring-boot-starter-amqp :通過 spring-rabbit 支持 AMQP。
- spring-boot-starter-actuator : 添加適用于生產(chǎn)環(huán)境的功能,如性能指標(biāo)和監(jiān)測(cè)等功能。
Java Config 自動(dòng)配置
Spring Boot 推薦采用基于 Java Config 的配置方式,而不是傳統(tǒng)的 XML。例如,@Configuration、@Bean、@EnableAutoConfiguration、@CompomentScan、@PropertySource、@Repository、@Service、@RestController等。
快速開始
1.新建一個(gè)空的maven項(xiàng)目
2.修改pom.xml,添加:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<!--開始搭建spring boot應(yīng)用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
<!--創(chuàng)建web項(xiàng)目要加入的依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
</dependencies>
3.新建java demo文件:
package com.cuteximi.go;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by 陶世磊 on 2017/7/2.
*
* @Description:
*/
@SpringBootApplication
public class ApplicationDemo {
public static void main(String[] args) throws Exception{
System.out.println("SpringApplication Run!");
SpringApplication.run(ApplicationDemo.class,args);
}
}
4.新建contorller demo
package com.cuteximi.go;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by 陶世磊 on 2017/7/2.
*
* @Description:
*/
@RestController
@EnableAutoConfiguration
public class RestfulApplicationDemo {
@RequestMapping("/")
public String home(){
return "Hello Spring Boot Web!";
}
@RequestMapping("/hello")
public String hello(){
return new String("hello");
}
public static void main(String[] args) throws Exception{
SpringApplication.run(RestfulApplicationDemo.class,args);
}
}
| 注解 | 區(qū)別 |
|---|---|
| @RestController | 該注解包含@Controller和@ResponseBody,里面的方法無法返回jsp頁(yè)面 |
| @Controller | 配合視圖解析器InternalResourceViewResolver使用,返回界面 |
| @ResponseBody | 返回JSON,XMl或者自定義的mediaType |
結(jié)尾附上pom.xml文件的內(nèi)容:
<?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>firstSpringBoot</groupId>
<artifactId>cuteximi</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<!--開始搭建spring boot應(yīng)用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
<!--創(chuàng)建web項(xiàng)目要加入的依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--該插件,運(yùn)行“mvn package”進(jìn)行打包時(shí),會(huì)打包成一個(gè)可以直接運(yùn)行的JAR文件,
使用java -jar命令就可以直接運(yùn)行-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
后面
機(jī)會(huì)是留給有準(zhǔn)備的人的!