SpringBoot基础配置

SpringBoot的相关配置

基础配置

复制工程(仅结构)

  • 复制项目
  • 进入项目中的pom.xml文件中,修改artifactId与新工程/模块名相同
  • 删除不必要的一些文件(仅保留src目录和pom文件)
  • 打开idea,在工程中将复制后的项目导入作为工程中的一个模块,并打开pom文件。如果第二步没有修改artifactId,此时必须修改,修改后刷maven依赖。
  • 注意一下pom文件中的name和description属性,对于现阶段来说没有什么太大的作用,建议删除。不然遇到模块中name相同的,在maven中就无法区分。(没有name就显示artifactId)
  • 保留备份工程后期使用

修改基本配置(port、banner、log)

Springboot的默认配置文件在哪呢?
在工程的resources下的application.properties,通过键值对配置对应属性
注意:Springboot中导入对应的start依赖后,才能够配置对应属性。在实际开发中,可采用关键字+提示形式进行书写配置。

# 服务器端口配置
server.port = 8080
# 修改banner',off-关闭,log-进到日志,console-打到控制台
spring.main.banner-mode = off/log/console
# 将banner设置成图片(将text.png放在resources路径下)
spring.banner.image.location = text.png
# 日志(根目录的日志级别,debug-编译模式,error-错误级别,info-默认的平常看到的)
logging.level.root = debug/error/info
# 如果想给某个包打日志级别
logging.level.com.taotao = warn

查所有配置?

进入spring官网,找到springboot ->learn 选择标绿版本 -> reference doc -> application properties

三种配置格式

  • application.properties
  • application.yml (主流)
  • application.yaml
    如果有相同的配置属性,配置文件会按照以下优先级进行加载。如果无重叠的配置属性,则会一并加载。
    优先级:properties > yml > yaml
    在yml中写配置的时候没有提示怎么办?
    file -> project structure -> facets 找到自己配置的工程 -> 点击右边最上面一个绿叶子的标志,点击加号,选中yml文件。

yaml语法规则

  • 大小写敏感
  • 属性层级关系使用多行描述,每行结尾使用冒号结束
  • 使用缩进表示层级关系,同层级左侧对齐,只允许用空格(不允许使用Tab键)
  • 属性值前面加空格(属性名与属性值之间使用冒号+空格作为分隔)
  • 表示注释

  • 数组表示方式:在属性名书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分开。
subject:
  - Java
  - 前端
  - 大数据
enterprise:
  name: itcast
  age: 16
  subject:
    - Java
    - 前端
    - 大数据

likes: [王者荣耀,刺激战场] #数组书写缩略格式

读取yaml中的数据

该如何读取上述配置中的值呢?

使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名…},如果数据存在多级,按照曾经一次书写即可。

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

    @Value("${subject[1]}")
    private String subject1;

    @GetMapping
    public void book(){
        System.out.println(enterprise1);
        System.out.println(subject1);
    }

运行结果
在这里插入图片描述

yaml中的变量引用

使用场景:多个属性的公共部分可以用一个公共属性来表示,也就是引用一个公共变量,这样该变量有变动的时候就不需要修改所有用到了它的属性。那么如何进行变量引用呢?

使用${属性名}引用变量

baseName: 陶
tempName: ${baseName}yy

// 使用
@Value("${tempName}")
    private String name;
    
@GetMapping
    public void book() {
        System.out.println("name===>" + name);
    }

运行结果:

name===>陶yy

如果属性值中出现了转义字符,并且你想让它生效,属性值必须使用双引号包起来,如下:

tempPath: "${baseName}\tyy\t1\t2\t3"

运行结果:

name===>陶 yy 1 2 3

读取yaml中全部属性

使用场景:一个个读取yaml中的属性在某些场景下十分繁琐,所以我们希望能够读取yaml中的全部属性,从而使用,那么我们如何读取呢?
使用自动装配@Autowired将所有的数据封装到一个Environment对象中。

@Autowired
private Environment env;

System.out.println(env.getProperty("enterprise.name"));
System.out.println(env.getProperty("tempPath"));

运行结果:

itcast
陶 yy 1 2 3

读取yaml引用类型的属性数据

使用场景:通常将配置文件中所有的属性都封装会显得很多余,对于开发者来说,封装自己需要的数据才是更加妥当的。
如何实现:首先我们需要创建一个类,用于封装需要的数据;其次,告诉spring这组数据,让它将这组数据加载到对象中。这样我们就可以从spring中取出数据并使用。
首先在yml文件中配置好属性,如下:

datasource:
  driver: com.mysql.jdbc.driver
  url: jdbc:mysql://localhost/springboot_db
  userName: root
  password: ******

其次创建相应的类

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

// 1、定义数据模型封装yml文件中对应的数据
// 2、定义spring管控的bean,也就是使用@Component注解将MyDataSource注入进spring容器
@Component
// 3、指定加载的数据
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {
    private String driver;

    private String url;

    private String userName;

    private String password;

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

然后在运行方法中打印,此处不展示代码了。运行结果如下:

MyDataSource{driver=‘com.mysql.jdbc.driver’, url=‘jdbc:mysql://localhost/springboot_db’, userName=‘root’, password=‘******’}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值