读取配置文件

@ConfigurationProperties

这是用来绑定配置文件的,把配置文件的属性绑定到 java 对象的字段上。这个 java 对象会被注册到容器中

有多种搭配方式:@EnableConfigurationProperties@Configuration@Component@Bean

搭配 @EnableConfigurationProperties

java 配置类:

// classpath: 表示当前工程 resource 目录(application.yml 的默认目录)
// classpath*: 当前工程和所有 jar 的 resource 目录(读取 jar 里面的配置文件)
// classpath:conf/db.properties:resource 的下级 conf 目录
@PropertySource("classpath:myApplication.yml") // 指定自定义配置文件位置和名称(不必须,如果不指定就是主配置文件)
@ConfigurationProperties(prefix = "person.test") // 和配置文件 person.test 的配置进行绑定
@Data 
public class MyConfig {
    private int id;
    private String name;
}

配置文件内容如下:

person.test.id=101
person.test.name=lisi

SpringBoot 启动类添加 @EnableConfigurationProperties 注解:

@EnableConfigurationProperties(MyConfig.class) // 启用 MyConfig 配置(这个注解可以不用写在启动类,只要能被 Spring 扫描到就行)
@SpringBootApplication
public class App {

}

搭配 @Configuration 等组件注解

@Configuration // @Component、@Controller、@RestController、@Service、@Repository 都可以
@ConfigurationProperties(prefix = "my.app") // 绑定主配置文件的 my.app 的配置
public class AppProperties {
    private String name;
    private int version;
    // getters & setters
}

搭配 @Bean

@Bean
@ConfigurationProperties(prefix = "database") // 绑定主配置文件的 database 的配置
public DataSource dataSource() {
    return new HikariDataSource();
}

自动提示

配置文件中编写配置时,根据绑定的 java 类的属性进行提示

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
 
<!-- 打成的 jar 中再排除,因为只是开发更直观,其实并没有也不影响 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

复杂示例:数组、集合、对象

主配置文件(yml 比 properties 支持的属性更复杂)

person: 
    id: 1
    name: Milk
    bobby: [吃饭, 睡觉, 打豆豆]
    family: [father, mother]
    map: {k1: v1, k2:v2}
    pet: {type: dog, name: 旺财} # 会根据 Pet 类自动映射

java 类

@Configuration 
@ConfigurationProperties(prefix = "person") // 注解表示要映射属性,并指定前缀
public class Person {
    private int id; 
    private String name; 
    private List<String> hobby;
    private String[] family; 
    private Map map;
    private Pet pet; // person.pet 配置项绑定到 Pet 类的字段上
}

class pet{
  	private String type;
  	private String name;
}

@Value

只能注入 JDK 已经有的类型,比如 String、List、Map 等,不能自定义类型(比如一个 POJO 类)

@PropertySource("my.config.properties")  // 默认到主配置文件的目录下找文件。不是必须的,如果没有就是读取主配置文件
@Configuration
public class myConfig{
    @Value("${spring.profiles.active}") // 如果配置文件不存在这个配置项会导致启动报错
    private String profile;

    @Value("${server.port}") // 读取整数类型
    private int port;

    @Value("${application.name:app}") // 指定默认值,配置文件不存在配置项也不会报错,使用默认值
    private String applicationName;
}

Spring 和 SpringBoot 表现不一致

@Value("${spring.profiles.active}") // 如果是Spring,配置文件没有这个配置启动不会报错,会把这个表达式直接设置到属性上
private String profile;
posted @ 2024-06-29 18:35  CyrusHuang  阅读(41)  评论(0)    收藏  举报