SpringBoot配置文件application.properties的理解

一、存放位置分类

1.当前项目根目录下的config目录下

2.当前项目的根目录下

3.resources目录下的config目录下

4.resources目录下

按照这上面的顺序,4个配置文件的优先级依次降低。

 我们在项目里面4个位置分别设置了各种的application.properties文件。每个文件都设置了各种的端口号,我们启动项目看到我们当前使用的端口号是优先级最高的项目根目录下config里面的配置。

 我们删除掉项目根目录下的config目录,现在项目使用的端口是项目根目录下的application.properties。

 二、自定义存放位置

如果我们不在这4个位置也想加载我们的配置文件的话,我们可以在resources目录下创建一个新目录命名为myconfig,目录下存放一个application.properties文件。我们可以在打包成jar的情况下在启动命令中加入配置文件的参数,就可以启动自定义的配置。

 java -jar xx.jar --spring.config.location=classpath:/myconfig/

三、自定义文件名

我们的application.properties文件名称也可以修改,比如修改成app.properties。我们可以在打成jar包的情况下在启动命令下加入配置文件名的参数,就可以使用自定义配置文件名。

 

java -jar SpringBootDemo-0.0.1-SNAPSHOT.jar --spring.config.name=app

我们看到现在项目使用的端口是app.properties文件下的8081端口了。 

四、属性注入

我们在application.properties文件中定义属性:

student.name=zhangsan
student.age=20

我们通过@Value注解把这些属性注入到我们的Student对象中:

示例代码如下:

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

/**
 * @author qinxun
 * @date 2023-06-15
 * @Descripion: 学生实体类
 */
@Component
public class Student {

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

    @Value("${student.age}")
    private Integer age;


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

测试:

import com.example.springbootdemo.bean.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootDemoApplicationTests {

    @Autowired
    private Student student;

    @Test
    void contextLoads() {
        // 输出 Student{name='zhangsan', age=20}
        System.out.println(student);
    }

}

五、类型安全的属性注入

示例代码如下:

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

/**
 * @author qinxun
 * @date 2023-06-15
 * @Descripion: 学生实体类
 */
@Component
@ConfigurationProperties(prefix = "student")
public class Student {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

这里我们主要引入@ConfigurationProperties(prefix="student")注解,并且配置了属性的前缀,此时自动将Spring容器中对应的数据注入到对象对应的属性中,不用通过@Value注解一个个注入。

配置文件:

student.name=lisi
student.age=20

测试: 

import com.example.springbootdemo.bean.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootDemoApplicationTests {

    @Autowired
    private Student student;

    @Test
    void contextLoads() {
        // 输出 Student{name='lisi', age=20}
        System.out.println(student);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinxun2008081

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值