3.5 使用属性文件

可以采用默认为我们配置的application.properties,也可以使用自定义的配置文件

引入属性文件依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

application.properties不用自己创建,应该是引入依赖后就自动创建了,在resources文件夹下在这里插入图片描述

在application.proterties中配置属性

database.driverName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/chapter3
database.username=root
database.password=123

新建类DataBaseProperties,通过**${……}这样的占位符读取配置在配置文件中的内容,这里@Value**注解可以加载属性,也可以用在方法上

package com.springboot.chapter3.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.awt.*;

/**
 * @author nmj
 * @create 2021-11-18 11:45
 */
@Component
public class DabaBaseProperties {
    @Value("${database.driverName}")
    private String driverName = null;

    @Value("${database.url}")
    private String url = null;

    private String username = null;

    private String password = null;

    public void setDriverName(String driverName) {
        System.out.println(driverName);
        this.driverName = driverName;
    }

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

    @Value("${database.username}")
    public void setUsername(String username) {
        System.out.println(username);
        this.username = username;
    }

    @Value("${database.password}")
    public void setPassword(String password) {
        System.out.println(password);
        this.password = password;
    }

    public String getDriverName() {
        return driverName;
    }

    public String getUrl() {
        return url;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

书中说启动SpringBoot后就能看到相关配置信息,但是我这里并没有
问题解决:之前没有在启动类上加@ComponentScan,没有将Bean加载进来
在这里插入图片描述

用注解**@ConfigurationProperties**减少配置代码

@Component
@ConfigurationProperties("database")
public class DabaBaseProperties {
    private String driverName = null;

    private String url = null;

    private String username = null;

    private String password = null;

    public void setDriverName(String driverName) {
        System.out.println(driverName);
        this.driverName = driverName;
    }

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

    public void setUsername(String username) {
        System.out.println(username);
        this.username = username;
    }

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

    public String getDriverName() {
        return driverName;
    }

    public String getUrl() {
        return url;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

这里在@ConfigurationProperties中配置的字符串database,将与POJO的属性名称组成属性的全限定名去配置文件里查找

有时候会把所有的内容都配置到application.properties,为了更好地配置,可以使用新的属性文件,例如,数据库的属性可以配置在jdbc.properties中,然后使用**@PropertySource**去定义对应的属性文件,把它加载到Spring的上下文

@SpringBootApplication
@ComponentScan(basePackages = "com.springboot.chapter3.pojo")
@PropertySource(value = {"classpath:jdbc.properties"}, ignoreResourceNotFound = true)
public class Chapter3Application {

    public static void main(String[] args) {
        SpringApplication.run(Chapter3Application.class, args);
    }

}

value可以配置多个配置文件,使用classpath前缀,意味着去类路径下找
在这里插入图片描述
ignoreResourceNotFound的默认值为false,也就是没有找到属性文件就会报错,这里设置为true,找不到就忽略

意外之喜:在这里插入图片描述
本来想尝试一下把jdbc.properties放到template文件夹(非类路径)中是否会出错,结果发现在移动的时候,@PropertySource中的value值也跟着变了,SpringBoot牛逼

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值