您的位置:首页 > 编程语言 > Java开发

学习笔记之微服务构建Spring Boot基础知识

2018-03-28 17:31 791 查看

快速入门

通过官方
Spring Initializr
能够快速构建基础项目。

新建项目,选择【Spring Initializr】、【Project SDK】版本以及【
https://start.spring.io




然后
Next
,填写
Group
Artifact
等信息,然后
Next
Next
Finish
就可以了。

可以在
pom.xml
中添加以下基础依赖:

<!--包含了Spring Core等模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!--包含tomcat,和SpringMVC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>


这样一个,一个简单的
SpringBoot
应用就完成了。可以创建
RESTful API
了。

例如新建:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@RequestMapping("/index")
public String index(){
return "hello world";
}
}




你可以在
http://localhost:8080/index
下访问到内容了。

小结:

starter POMs
是一系列轻便的依赖包,是一套一站式的Spring相关技术的解决方案。在使用
Spring Boot
构建应用的时候,各项功能模块的整合不再像传统的
Spring
应用的开发方式那样,需要在
pom.xml
中做大量的依赖配置,而是通过使用
starter POMs
定义的依赖包,使得功能模块整合变得非常轻巧,易于理解和使用。

配置详解

Spring Boot
的默认配置文件位置为
src/main/resources/application.properties
,根据我们引入的不同的
Starter模块
,可以在这里定义容器的端口号、数据库连接信息、日志级别等各种配置信息。比如配置
server.port=8866
来指定服务端的端口为8866等。
Spring Boot
配置文件推荐使用YAML文件。

.yml
文件的如下内容:

spring:
application:
name: spring-boot-stady
server:
port: 8866


等价于
.properties
中的这样:

server.port=8866
spring.application.name=spring-boot-stady


YAML的配置方式是利用阶梯化的缩进方式,其结构更为清晰易读。

自定义参数

除了可以在
Spring Boot
的配置文件中设置各个
Starter
模块中
预定义的配置属性
,也可以在配置文件中定义一些我们需要的
自定义属性
。如在
application.properties
中添加:

school.name=hubeiligong
school.addr=hubeihuangshi


然后在应用中可以通过@Value注解来加载这些自定义的参数:

public class HelloController {

@Value("${school.name}")
private String name;

@Value("${school.addr}")
private String addr;

@RequestMapping("/index")
public String index(){
return "hello world"  + name + addr;
}
}


参数引用

application.properties
中的各个参数之间可以直接通过使用
PlaceHolder
的方式来进行引用。如下面这样:

school.name=hubeiligong
school.addr=hubeihuangshi
school.desc= ${school.name} is located at ${school.addr}


school.desc
参数的结果是
hubeiligong is located at hubeihuangshi


使用随机数

可以不用通过程序,而通过配置随机生成属性。

# 随机字符
name=${random.value}
# 随机int
number=${random.int}
# 随机long
bignumber=${random.long}
# 10以内的随机数
test1=${random.int(10)}
# 10~20的随机数
test2=${random.int[10,20]}


该配置方式可以设置应用端口等场景,以避免在本地调试时出现端口冲突的麻烦。

命令行参数

例子:直接以命令行的方式设置
server.port
属性,并启动应用,端口为8867:

java -jar xxx-yyy.jar --server.port=8867


在用命令行方式启动SpringBoot应用是,连续的两个减号
--
就是对
application.properties
中属性值进行赋值的标识。所以
java -jar xxx-yyy.jar --server.port=8867
命令,等价于在
application.properties
中添加属性
server.port=8867


多环境配置

在Spring Boot中,多环境配置的文件名需要满足
application-{profile}.properties
的格式,其中
{profile}
对应环境标识,如下:

application-dev.properties:开发环境。

application-test.properties:测试环境。

application-prod.properties:生产环境。

至于具体哪个配置文件会被加载,需要在
application.properties
文件中通过
spring.profiles.active
属性来设置,其值对应配置文件的
{profile}
值。

多环境配置思路:

application.properties
文件配置通用内容,并设置
spring.profiles.active=dev
,以
开发环境
为默认配置。

application-{profile}.properties
中配置各个环境不同的内容。

通过
命令行方式
去激活不同的环境。

加载顺序

1、 在命令行中传入的参数

2、
SPRING_APPLICATION_JSON
中的属性。
SPRING_APPLICATION_JSON
是以
JSON
格式配置在系统环境变量中的内容。

3、
java:comp/env
中的
JNDI
属性。

4、 java的系统属性,可以通过
System.getProperties()
获得的内容。

5、 操作系统的环境变量。

6、 位于当前应用jar之外,针对不同
{profile}环境
的配置文件内容。

7、 位于当前应用jar之内,针对不同
{profile}环境
的配置文件内容。

8、 位于当前应用jar之外的
aplication.properties
YAML
配置内容。

9、 位于当前应用jar之内的
aplication.properties
YAML
配置内容。


10、在
@Configuration
注解修改的类中,通过
@PropertySource
注解定义的属性。


11、 应用默认属性,使用
SpringApplication.setDefaultProperties
定义的内容。

优先级按上面的顺序由高到低,数字越小,优先级越高。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: