目录
1.简介
它使用“习惯优于配置”(项目中存在大量的配置,此外还内置一个习惯性的配置,让你无须)的理念让你的项目快速运行起来。它并不是什么新的框架,而是默认配置了很多框架的使用方式,就像Maven整合了所有的jar包一样,Spring Boot整合了所有框架
2.手动搭建spring boot项目
(1)新建maven项目
(2)添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
(3)编写启动类
package com.demo.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.idea搭建spring boot项目
(1)新建项目,选择Spring Initializr,然后选择默认的url点击【下一个】
(2)然后修改一下项目的信息
(3)勾选上Web模板
(4)选择好项目的位置,点击【完成】
4. Eclipse搭建spring boot项目
(1)下载安装Eclipse的STS插件
(2)选择新建项目 - 春 - 春启动项目,新建春启项目。
5.控制器的编写
(1)新建类,类上添加注解
@RestController //@Controller和@ResponseBody注解的合体版
@RequestMapping("xxx") // 指明该类访问路径,可不写
(2)新建方法,方法上加注解
@RequestMapping(value="yyy") // 指明该ff访问路径
(3)启动启动类可启动项目(main方法)
(4)访问路径为:/ xxx / yyy
6. spring boot的相关配置
(1)简介
Spring Boot使用一个全局的配置文件application.properties或application.yml,放置在【src / main / resources】目录或者类路径的/配置。
Spring Boot不仅支持常规的属性配置文件,还支持yaml语言的配置文件.yaml是以数据为中心的语言,在配置数据的时候具有面向对象的特征。
(2)默认配置的配置值进行修改
#配置程序端口,默认为8080
server.port=40000
#session过期时间,以秒为单位
server.session.timeout=1800
#配置默认访问路径,默认为/
server.context-path=/
#配置Tomcat编码,默认为UTF-8
server.tomcat.uri-encoding=UTF-8
#配置最大线程数 默认为200
server.tomcat.max-threads=1000
(3)配置信息获取
定义变量,上面加注解@Value( “$ {XXXX}”),配置文件在项目启动时加载
封装配置信息,新建实体类,类上加注解
@Component
//prefix 表示获取前缀为xxx的配置信息,添加set/get方法 locations 表示配置文件路径,src/main/resources目录下
@ConfigurationProperties(locations = "classpath:mail.properties",prefix = "xxx")
Environment对象获取
//引入Environment 变量
@Resource
private Environment env;
//在方法中获取配置信息
env.getProperty("name")
字段为配置key的剩余部分,如:student.name - > student为前缀,name为实体类字段
(6) 使用随机数
在spring boot的属性配置文件中,可以通过使用${random}配置来产生随机的int值、long值或string字符串
(7) 多环境配置
在spring boot中,多环境配置的文件名需要满足 application-{profile}.properties的格式,其中{profile}对应你的环境标识。
application-dev.properties:开发环境
在application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认,在application-{profile}.properties中配置各个环境不同的内容,通过命令行方式去激活不同环境的配置。
7. spring boot打包
(1)打jar包
1)pom文件配置
<packaging>jar</packaging>
<build>
<finalName>bg_anaytics</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2)执行maven的package命令,生成jar包到 target目录下
3)执行 java -jar xxxxx.jar 启动项目
(2)打war包
1)pom文件配置
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
2)添加ServletInitializer
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
//由于我们采用web3.0 规范,是没有web.xml 的,而此类的作用与web.xml相同。
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
3)执行maven的package命令,生成war包到 target目录下
4)将war 包放入tomcat,启动tomcat