(推荐):jdk1.8+Maven(3.2+)+Intellij IDEA+windows10;html
说明:前端
jdk:springboot官方说的很明确,到目前版本的springboot(1.5.9),官方指定要求jdk1.8以上;vue
依赖包管理:能够经过拷贝jar文件的方式管理依赖,但官方也推荐使用Apache Maven 3.2或更高版本等构件工具;java
开发工具:我的推荐使用IDEA,功能很强大,使用流畅度和方便性较好;react
本节目标:构造第一个springboot入门web项目,经过三种方式运行启动;并经过浏览器获得服务器反馈结果;git
步骤:github
确保在jdk1.8和maven3.2+;web
进入https://start.spring.io/,选择要使用的springboot版本号,这里使用1.5.9,填写好项目组信息,在Search for dependencies中搜索web并选中;redis
选择生成项目后自动下载chapter01.zip;解压chapter01.zip后,使用IDEA打开chapter01项目文件夹;spring
此外,还能够在IDEA中File-new-Project,选择Spring Initializr,在IDEA中建立springboot项目;
(注意:此处若是你的项目报错,请确保该项目的maven配置正确;IDEA的话打开,正确指定好使用的maven是3.2以上版本便可:
)
项目正常打开后的目录结构以下:
Chapter01Application.java 内部包含main函数,是springboot项目的启动类;
Chapter01ApplicationTests.java 测试类
pom.xml 依赖管理文件
application.properties 配置文件,初次生成的时候是空的,之后能够在里面填写配置项;
有的同窗到这里有些懵,以往java web项目不是有个WebRoot文件夹吗,这里我明明配置的就是web项目,为何会是这么个目录结构呢?
这里其实就是springboot提倡的习惯,之后开发springboot,要习惯使用模板,摒弃jsp的前端方案(若是你执意要使用jsp的话也是能够的,咱们后文会介绍);如今咱们项目组开发,基本全是freemarker,并且基本上所有都是请求rest 接口,这样先后端彻底分离;避免了在jsp中写java代码的陋习(记得之前作过一个H5支付页面,用jsp写的,后来项目升级,前端用vue.js,结果后台须要从新写n多接口);之后项目想更换前端方案也不会是坑;因此要接受并学习使用模板;要完全执行先后端分离的思想;
之前使用jsp时,你会把jsp放在WEB-INF下面;之后使用模板,你须要把模板全放到例如resources-templates下面去;
sprinboot到底使用什么模板,因人而异;我我的喜欢freemarker;
以freemarker为例,模板.ftl文件会被放到resources.templates中,其余静态资源文件会被放到resources-static中,这块往后再婊;
咱们把目光先集中在maven的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.zjt</groupId> <artifactId>chapter01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>chapter01</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
是否是看上去很简单,其实里面大有玄机;
首先,注意打包文件格式是jar文件,这是由于,sprinboot使用mvn插件能够打包成可执行的内嵌web容器的jar,这对于发布微服务是很方便的;若是想打成war包和其余项目一块儿集成在tomcat中,也是能够的,这个往后再表;
构建项目:
方法1:父依赖(spring initializr默认构建方式):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository -->
</parent>
此处,咱们发现springboot官方默认使用父依赖的方式来构件springboot项目。ctrl+左键点击跟踪spring-boot-starter-parent,会发现其实她继承了spring-boot-dependencies;包括往后咱们经常使用的配置文件的过滤等
总结起来,能够说spring-boot-starter-parent,有以下特性:
默认编译级别为Java 1.8 源码编码为UTF-8 一个依赖管理节点,容许你省略普通依赖的 <version> 标签,继承自 spring-boot-dependencies POM。 合适的资源过滤 合适的插件配置(exec插件,surefire,Git commit ID,shade) 针对 application.properties 和 application.yml 的资源过滤
只要指定了父依赖的version,那么其相关的其余自动依赖均无需再指定版本号,springboot已经自动管理好了最佳的依赖配置,如图:
这也是springboot的方便之处;
除非你手动覆盖本身的项目中的属性,来达到修改某个依赖的版本号的目的;
方法2:使用import来引入spring-boot-dependencies
若是不想使用父依赖的方式,能够直接经过使用一个 scope=import 的依赖来构建:
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.0.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Starter POMS :
下面咱们看一下pom.xml中的依赖模块,发现有一些颇有特点的spring-boot-starter-*,官方学名叫starter poms;
springboot使用各类starter poms来实现组件的热插拔;每个starter pom就是一个能够包含到应用中的一个方便的依赖关系描述符集合;能够获取全部Spring及相关技术的一站式服务,而不须要翻阅示例代码,拷贝粘贴大量的依赖描述符。
例如,若是你想使用Spring和JPA进行数据库访问,只须要在你的项目中包含 spring-boot-starter-data-jpa 依赖,而后你就能够开始了。
下面的应用程序starters是Spring Boot在 org.springframework.boot 组下提供的,咱们能够方便地查找你的项目须要的其余组件的starter,直接添加便可自动加载依赖:
Table 13.1. Spring Boot application starters
Name | Description | Pom |
---|---|---|
Core starter, including auto-configuration support, logging and YAML 核心Spring Boot starter,包括自动配置支持,日志和YAML |
||
Starter for JMS messaging using Apache ActiveMQ |
||
Starter for using Spring AMQP and Rabbit MQ 对"高级消息队列协议"的支持,经过 spring-rabbit 实现 |
||
Starter for aspect-oriented programming with Spring AOP and AspectJ 对面向切面编程的支持,包括 spring-aop 和AspectJ |
||
Starter for JMS messaging using Apache Artemis |
||
Starter for using Spring Batch 对Spring Batch的支持,包括HSQLDB数据库 |
||
Starter for using Spring Framework’s caching support |
||
Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku 对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的链接 |
||
Starter for using Cassandra distributed database and Spring Data Cassandra |
||
Starter for using Cassandra distributed database and Spring Data Cassandra Reactive |
||
Starter for using Couchbase document-oriented database and Spring Data Couchbase |
||
Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive |
||
Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch 对Elasticsearch搜索和分析引擎的支持,包括 spring-data-elasticsearch |
||
Starter for using Spring Data JPA with Hibernate 对"Java持久化API"的支持,包括 spring-data-jpa , spring-orm 和Hibernate |
||
Starter for using Spring Data LDAP |
||
Starter for using MongoDB document-oriented database and Spring Data MongoDB 对MongoDB NOSQL数据库的支持,包括 spring-data-mongodb |
||
Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive |
||
Starter for using Neo4j graph database and Spring Data Neo4j |
||
Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client |
||
Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client |
||
Starter for exposing Spring Data repositories over REST using Spring Data REST 对经过REST暴露Spring Data仓库的支持,经过 spring-data-rest-webmvc 实现 |
||
Starter for using the Apache Solr search platform with Spring Data Solr 对Apache Solr搜索平台的支持,包括 spring-data-solr |
||
Starter for building MVC web applications using FreeMarker views 对FreeMarker模板引擎的支持 |
||
Starter for building MVC web applications using Groovy Templates views 对Groovy模板引擎的支持 |
||
Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS 对基于HATEOAS的RESTful服务的支持,经过 spring-hateoas 实现 |
||
Starter for using Spring Integration 对普通 spring-integration 模块的支持 |
||
Starter for using JDBC with the Tomcat JDBC connection pool 对JDBC数据库的支持 |
||
Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to 对Jersey RESTful Web服务框架的支持 |
||
Starter for using jOOQ to access SQL databases. An alternative to |
||
Starter for reading and writing json |
||
Starter for JTA transactions using Atomikos 对JTA分布式事务的支持,经过Atomikos实现 |
||
Starter for JTA transactions using Bitronix 对JTA分布式事务的支持,经过Bitronix实现 |
||
Spring Boot Narayana JTA Starter |
||
Starter for using Java Mail and Spring Framework’s email sending support 对 javax.mail 的支持 |
||
Starter for building web applications using Mustache views 对Mustache模板引擎的支持 |
||
Spring Boot Quartz Starter |
||
Starter for using Spring Security 对 spring-security 的支持 |
||
Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito |
||
Starter for building MVC web applications using Thymeleaf views 对Thymeleaf模板引擎的支持,包括和Spring的集成 |
||
Starter for using Java Bean Validation with Hibernate Validator |
||
Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container 对全栈web开发的支持,包括Tomcat和 spring-webmvc |
||
Starter for using Spring Web Services |
||
Starter for building WebFlux applications using Spring Framework’s Reactive Web support |
||
Starter for building WebSocket applications using Spring Framework’s WebSocket support 对WebSocket开发的支持 |
除了应用程序的starters,下面的starters能够用于添加生产准备的特性:
Table 13.2. Spring Boot production starters
Name | Description | Pom |
---|---|---|
Starter for using Spring Boot’s Actuator which provides production ready features to help you monitor and manage your application 添加生产准备特性,好比指标和监控 |
最后,Spring Boot包含一些可用于排除或交换具体技术方面的starters。
Table 13.3. Spring Boot technical starters
Name | Description | Pom |
---|---|---|
Starter for using Jetty as the embedded servlet container. An alternative to 导入Jetty HTTP引擎(做为Tomcat的替代) |
||
Starter for using Log4j2 for logging. An alternative to 对Log4J日志系统的支持 |
||
Starter for logging using Logback. Default logging starter 导入Spring Boot的默认日志系统(Logback) |
||
Starter for using Reactor Netty as the embedded reactive HTTP server. |
||
Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by 导入Spring Boot的默认HTTP引擎(Tomcat) |
||
Starter for using Undertow as the embedded servlet container. An alternative to 导入Undertow HTTP引擎(做为Tomcat的替代) |
使用maven插件用来打包springboot项目:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>
</build>
项目启动类(Chapter01Application.java):
package com.zjt.chapter01; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Chapter01Application { public static void main(String[] args) { SpringApplication.run(Chapter01Application.class, args); } }
上述Chapter01Application.java为springboot的main启动类;
这里咱们开始学习到了springboot启动类中的最重要的一个注解:@SpringBootApplication
注意:
1.项目启动类,根据官方建议,必定要位于非 default package下;也就是说这个启动类必定要含有package的声明;建议使用反转域名;如com.zjt.learn;
2.官方建议,main应用类要放在其余类上面的根包(root package)中;
3.@SpringBootApplication该注解在基于上述1和2两点下,才可使用(习惯优于配置);
因为该注解至关于:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
_______________华丽的小分割__________________
@SpringBootConfiguration:说明当前类是一个配置类,就像xml配置文件,而如今是用java配置文件,效果是同样的,它会被@ComponentScan扫描到。
@EnableAutoConfiguration(开启自动配置,springboot的灵魂所在)注解一般都放到main所在类的上面,当main所在类位于root package的时候,这样@EnableAutoConfiguration能够从逐层的往下搜索各个加注解的类,例如,你正在编写一个JPA程序(若是你的pom里进行了配置的话),spring会自动去搜索加了@Entity注解的类,并进行调用;
@ComponentScan:用注解配置实现自动扫描,默认会扫描当前包和全部子包,和xml配置自动扫描效果同样;
___________________________________________
因此咱们的springboot很贴心的为咱们准备了Springboot 提供了统一的注解@SpringBootApplication来替代以上三个注解,简化程序的配置;
package com.zjt.chapter01.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping("/hello") public String Hello(){ return "hello world"; } }
看到这样就证实已经顺利启动了;
2.4.2使用mvn spring-boot:run”在命令行启动;
2.4.3使用mvn package打成可执行jar;使用java -jar启动;
mvn clean ;mvn build; mvn package;
打包后,命令行启动:
OK,今天的入门咱们就一块儿作到这里;后续会同你们共同探讨不少springboot的实用教程和案例;文章中有什么问题或者想法欢迎老铁们吐槽和我沟通;我会及时纠正错误,改进教程的品质;
作程序须要静心,扎扎实实作技术;不忘初心,脚踏实地,就酱。