欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

SpringBoot配置文件application.properties的使用,

来源: javaer 分享于  点击 31088 次 点评:95

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);
    }
 
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持3672js教程。

您可能感兴趣的文章:
  • SpringBoot加载多个配置文件实现dev、product多环境切换的方法
  • SpringBoot实现多环境配置文件切换教程详解
  • SpringBoot如何配置文件给bean赋值问题
  • Springboot maven项目配置文件覆盖问题的处理
  • SpringBoot如何读取application.properties配置文件
  • SpringBoot中配置文件及切换方式
相关栏目:

用户点评