一、什么是springboot?java
简单的理解就是一个快速开发框架集合,你们都叫他快速开发脚手架。web
二、springboot有什么特征?spring
它遵循“习惯因为配置”原则,使用springboot只须要少许配置,咱们大部分时候可使用他的默认配置;apache
它可让咱们搭建项目更快速,可无配置整合第三方框架;设计模式
彻底不使用xml配置,只是用自动配置或是Java configtomcat
它内嵌了servelt容器(tomcat、jetty),应用能够用jar的方式运行springboot
集成了应用运行状态的监控app
三、学习springboot须要什么知识储备?框架
java这个不用说了,哈哈maven
maven这个必须会用啦
spring 、spring MVC 这两个是最基本的,这里的spring 必须是 4.x版本,由于springboot使用了spring4.x 的@Conditional注解。
想要学好它你还必须对设计模式有必定理解,还有core java中的 反射、注解、泛型 这些必不可少了,这部分有助于咱们去理解源码。
一、 新建一个maven工程,你们能够直接到spring官网上去新建一个,下载下来,或是本身建一个。我提倡手动。
二、引入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; @SpringBootApplication public 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风格的访问路径