SpringBoot配置文件的使用

一.全局文件配置

Spring Boot 使用全局配置文件来允许开发者自定义应用程序的配置。这些配置文件可以用来修改自动配置的设置,或者添加新的配置项。

配置文件的位置和命名:application.properties 或 application.yml:

    • 默认情况下,Spring Boot 会在以下位置查找配置文件,并按照顺序加载它们(后面的会覆盖前面的配置):
      • 当前目录下的 /config 子目录。
      • 当前目录。
      • classpath 下的 /config 包及其子目录。
      • classpath 根路径。

两种配置文件示例

application.properties

spring.application.name=springBoot01

#系统默认配置修改,修改web端口
server.port=9000
#自定义配置
student.id=1000
student.name=maming
student.sex=boy
#数组或者List集合
student.users=[mmm,aaaa,ccc]
#双列集合
student.names.name1=maming
student.names.name2=liuming
student.names.name3=zhangsan

 

 

properties类的文件使用.符号进行定义从级关系,同时使用:号进行值写入

application.yml

#yml语法使用行缩进确定从级关系
server:
  port:9000
spring:
  mvc:
    servlet:
      path: /hello/


#自定义对象
student:
  name:maming
  sex:boy
  id:18
#数组或List集合 杠(-)后面也有一个空格缩进
users:
  - maming
  - liuming
  - zhangsan
#map集合
names:
  name1:maming
  name2:liuming
  name3:zhangsan

 

 

使用yml最好切换为英文输入,使用两个英文空格就构造出一个子父级关系,使用:号进行赋值,同级不需要空格缩进

使用这两种配置文件都需要注意,是区分大小写的,大写的Server和小写的server是不一样的,注意区分

在填写信息的时候需要注意,不区分字符数字,都不需要加字符串引号,取出的时候数据都默认为字符串,如果确实像age这类为数字时,需要在取出时进行转换 

代码取配置文件的值

@Value注解

在Spring Boot中,@Value 注解是一个非常有用的工具,用于将外部配置属性值注入到Spring管理的Bean中。它支持从多种来源获取值,包括properties文件、YAML文件、系统环境变量

例如:

在application.properties文件中配置一个student对象:

student.id=1000
student.name=maming
student.sex=boy

 

在student类中使用@Value注解注入

@Component
public class Student {
    @Value("${student.id}")
    private Integer id;
    @Value("${student.name}")
    private String name;
    @Value("${student.sex}")
    private String sex;
}

 

 

@ConfigurationProperties注解

@ConfigurationProperties 是 Spring Boot 提供的一个非常有用的注解,用于将配置文件中的属性绑定到 Java 对象上。这样可以使配置管理更加方便和类型安全。

使用这个注解可以一下匹配一个类下所有的属性,使用的时候需要注意,由于在properties中存储的时候都有前缀,使用注解的时候需要配置一下

@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    private Integer id;
    private String name;
    private String sex;
}

 

Environment对象

在 Spring Boot 中,Environment 对象是一个核心接口,用于访问应用程序的环境配置。它提供了对各种配置属性的抽象,包括从 application.propertiesapplication.yml 文件中读取的属性、系统属性、环境变量等。通过 Environment 对象,你可以动态地获取和操作这些配置信息。

    @Autowired
    private Environment env;
    @Test
    void contextLoads() {
        System.out.println(env.getProperty("student.id"));
        System.out.println(env.getProperty("student.name"));
        System.out.println(env.getProperty("student.sex"));
    }

 

测试结果:

二.定义配置类 

配置类(Configuration Class)用于定义和配置应用程序的各种 Bean。通过使用 @Configuration 注解,你可以创建一个配置类,在其中声明 Bean 并指定它们的依赖关系。

配置类的主要作用包括:

  1. Bean 定义:定义应用程序所需的 Bean。
  2. 依赖注入:管理 Bean 之间的依赖关系。
  3. 配置属性绑定:将配置文件中的属性绑定到 Java 对象上。
  4. 条件化配置:根据某些条件启用或禁用 Bean 的注册。

在上面的测试中我们注入学生类的Bean使用的是@Componet注解,这种注解方式可以快速将类注入到spring容器中,但是这种方式不能控制创建Bean的制造过程,如果我们想要自定义Bean的初始化属性那么还是需要使用@Bean注解,他需要配置@Configuration注解

@Configuration
public class CreateBean {
    @Bean
    public Student getStudent(){
        Student student = new Student();
        student.setId(100);
        student.setName("zhangsan");
        return student;
    }
}

 

再次测试这个Bean的输出值:

Student{id=100, name='zhangsan', sex='null'}

 

由于注入Bean时我们并没有初始化sex属性,故而为null

 

 

------END-----

posted @ 2025-03-16 17:49  回忆也交给时间  阅读(67)  评论(0)    收藏  举报