1.使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程
菜单栏中选中Files->New->Projects,这个Initializr Service URL 我们一般使用默认,
也就是spring官方提供的spring Initializr工具进行创建
点击下面的Next之后,稍等片刻,出现下面的页面,我们可以通过
Language选项更改编写语言,Type选项更改构建的工程类型
点击Next选项,进入到springboot的版本选择和依赖管理,除了springboot的依赖管理还能看到springcloud的依赖管理
再点击Next,进入到创建的最后一步,可以项目的名称,项目地址,点击Finsh完成项目创建
2.SpringBoot之HelloWorld的项目详解
springboot的中文意义就是引导的意思,就是简化spring应用从搭建到开发的过程,即开箱即用,只要通过java-jar或者Tomcat或者maven的插件run或者shell脚本就可以启动项目,springboot只要很少的应用配置,遵循这习惯大于配置的原则,
创建SpringBoot的项目,已经在上面说过了,创建SpringBoot的web项目,需要选中web即可,工程的类型我们一般都是maven Properject
项目的POM文件:
<groupId>com.example</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springbootdemo</name>
<description>Demo project for Spring Boot</description>
<parent><!--springboot 启动父依赖-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties><!--springboot项目的默认编码格式和jdk版本-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies><!--springboot 的web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><!--springboot的test依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!--springboot的maven打包插件-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
项目的controller文件
@RestController
public class helloController {
@RequestMapping("/")
public String Hello(){
return "Hello SpringBoot";
}
}
@RestController和@RequestMapping注解是来自springMVC,他们不是springboot的特定部分
@RestController可以服务于JSON和XML或者其他,这里是以spring的形式进行结果渲染
@RequestMapping提供路由信息,将“/”路径的HTTP Request映射到Hello方法进行处理
项目的启动类文件
/**
* springboot项目的启动类文件
*/
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
@SpringBootApplication是springboot应用的标识,SpringbootdemoApplication很简单,一个main方法,springApplication引导应用,将SpringbootdemoApplication自身当作参数传递给run方法,具体的run方法会启动嵌入式的Tomcat并初始化Spring环境和Spring组件
项目的测试文件
一个好的项目,是需要测试Test的
/**
* Spring Boot HelloWorldController 测试 - {@link HelloWorldController}
*
* Created by bysocket on 18/7/30.
*/
public class HelloWorldControllerTest {
@Test
public void testSayHello() {
assertEquals("Hello,SpringBoot!",new helloController ().Hello());
}
}
3.SpringBoot的配置文件详解,自定义属性,随机数,多环境配置
3.1自定义属性
在使用Spring Boot的时候,通常也是需要定义一些自己使用的属性的,
在配置文件application.properties中可以这样写:
com.didispace.blog.name=程序员
com.didispace.blog.title=spring boot
然后通过@Value("${属性名}")注解来加载对应的配置属性
@Component
public class BlogProperties {
@Value("${com.didispace.blog.name}")
private String name;
@Value("${com.didispace.blog.title}")
private String title;
// 省略getter和setter
}
3.2参数间的引用
在配置文件application.properties中各个参数之间也可以直接引用
com.didispace.blog.name=程序猿
com.didispace.blog.title=Spring Boot
com.didispace.blog.desc=${com.didispace.blog.name}正在努力写《${com.didispace.blog.title}》
3.3使用随机数
有时候,有些参数我们希望它不是一个固定值,比如密钥,服务器端口,spring boot 的属性配置文件中可以通过
${random}来生产int值,long值或者String字符串,来支持属性的随机值
# 随机字符串
com.didispace.blog.value=${random.value}
# 随机int
com.didispace.blog.number=${random.int}
# 随机long
com.didispace.blog.bignumber=${random.long}
# 10以内的随机数
com.didispace.blog.test1=${random.int(10)}
# 10-20的随机数
com.didispace.blog.test2=${random.int[10,20]}
3.4通过命令行设置属性值
在命令行运行时,连续的俩个减号--就是对application.properties中的属性值进行赋值的标识,
例如
java -jar xxx.jar --server.port=8888命令,等价于我们在application.properties中添加属性server.port=8888,
也可以通过设置下面的代码来屏蔽命令行改变应用运行的参数
SpringApplication.setAddCommandLineProperties(false)。
3.5多环境配置
在开发springboot应用的时候,通常会用到几个不同的环境,比如开发环境,测试环境,生产环境等
在springboot配置多环境文件名时,需要满足application-{profile}.properties的格式,其中profile对应环境标识,
比如 application-dev.properties 开发环境
application-test.properties 测试环境
application-prod.properties 生产环境
至于那个具体的配置文件被加载,需要在application.properties文件中通过
spring.profiles.active属性进行配置,其值对应{profile}的值
小结:常用的springboot样板配置已经在springboot的官方文档中给出
http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html