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

快速入门

通过官方Spring Initializr能够快速构建基础项目。
新建项目,选择【Spring Initializr】、【Project SDK】版本以及【https://start.spring.io

然后Next ,填写GroupArtifact等信息,然后NextNextFinish就可以了。
可以在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.propertiesYAML配置内容。
9、 位于当前应用jar之内的aplication.propertiesYAML配置内容。
10、在@Configuration注解修改的类中,通过@PropertySource注解定义的属性。
11、 应用默认属性,使用SpringApplication.setDefaultProperties定义的内容。

优先级按上面的顺序由高到低,数字越小,优先级越高。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值