1. 简介
Spring Boot是Spring团队推出的新框架,它所使用的核心技术还是Spring框架,主要是Spring 4.x,所以如果熟悉spring 4的人,能够更快的接受和学会这个框架。Spring boot可以看做是在spring框架基础上再包了一层,这一层包含方便开发者进行配置管理和快速开发的模块,以及提供了一些开箱即用的工具,比如监控等。
Spring Boot让我们的Spring应用变的更轻量化。比如:你可以仅仅依靠一个Java类来运行一个Spring引用。你也可以打包你的应用为jar并通过使用java -jar来运行你的Spring Web应用。
Spring Boot的主要优点:
为所有Spring开发者更快的入门
开箱即用,提供各种默认配置来简化项目配置
内嵌式容器简化Web项目
没有冗余代码生成和XML配置的要求
2.快速入门
主要目标完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单、开发快速的特性。
系统要求:
Java 7及以上
Spring Framework 4.1.5及以上
本文采用Java 1.8.0_102、Spring Boot 1.3.2、maven构建项目 调试通过。
项目结构解析
为了规范合理,建立一个父工程,接下来将通过增加子模块的方式,来系统学习。
父工程的pom文件
<?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.springboot</groupId>
<artifactId>zjl</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>example01</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<springboot.version>1.3.2.RELEASE</springboot.version>
<swagger2.version>2.2.2</swagger2.version>
<mysql.version>5.1.21</mysql.version>
<mybatis.version>1.1.1</mybatis.version>
<mongodb.version>3.2.2</mongodb.version>
</properties>
<dependencies>
<!-- springboot 基础包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<!-- <exclusions> 引入log4j日志时需去掉默认的logback <exclusion> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> -->
</dependency>
<!-- stateMachine框架 -->
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-core</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<!-- 日志管理 log4j -->
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId>
</dependency> -->
<!-- springboot 测试包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- springboot web包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot 发送邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- springboot web开发velocity模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
<!-- 监控管理actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
example01的pom文件
<?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">
<parent>
<artifactId>zjl</artifactId>
<groupId>com.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>example01</artifactId>
</project>
如果maven中创建不了java类,就进行一下操作,将文件夹变换成SourceRoot
编写类:
Main.java
package com.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.web.SpringBootServletInitializer;
/**
* @Author : xiaolin
* @Description: Created by Administrator on 2017/11/22.
*/
@SpringBootApplication
public class Main extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(90); //设置启动端口号
}
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
HelloController.java
package com.lyd.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(){
return "Hello World";
}
}
右键main方法运行、浏览器中访问localhost:90/hello