springboot读取配置文件属性注解@Value、@PropertySource、@ConfigurationProperties、@EnableConfigurationProperties

807 阅读2分钟

前沿介绍

SpringBoot中,读取配置文件属性的相关注解有:

  • @Value
  • @PropertySource
  • @ConfigurationProperties

接下来,我们对其进行简单的案例介绍:

@Value

功能:@Value先读默认配置文件application.properties中定义的属性。

使用方式@value(占位符) 该注解加载成员变量上
举例@value("${user.name}")

application.properties中配置:

user.username=csp
user.password=123
user.age=22

java代码读取:

@Data// lombok插件注解,可替换成setter/getter
@AllArgsConstructor// lombok插件注解,可替换成全参构造函数
@NoArgsConstructor// lombok插件注解,可替换成空参构造函数
@Component
public class User {
    @Value("${user.username}")
    private String username;//csp
    @Value("${user.password}")
    private String password;//123
    @Value("${user.age}")
    private int age;//22
}

@PropertySource

功能:@PropertySource读取自定义的配置文件中定义的属性。

使用方式@PropertySource(value={classpath:/指定配置文件名称}) 该注解加载成员变量上
举例@PropertySource(value={"classpath:/user.properties"})

user.properties中配置:

user.username=csp1999
user.password=123456
user.age=22

java代码:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@PropertySource(value={"classpath:/user.properties"})
public class User {
    @Value("${user.username}")
    private String username;//csp1999
    @Value("${user.password}")
    private String password;//123456
    @Value("${user.age}")
    private int age;//22
}

@ConfigurationProperties

功能:@ConfigurationProperties读取默认配置文件中定义的属性,但是可以指定属性前缀。

使用方式@ConfigurationProperties(prefix = "前缀名称") 该注解加载成员变量上
举例@ConfigurationProperties(prefix = "user")

默认配置文件application.properties/application.yml中配置:

user.username=csp789
user.password=456
user.age=18

注意:该注解在新版本的springboot中idea窗口会出现
在这里插入图片描述
这种提示,解决方法1:

<!--
   个人版本踩坑:
   不加这个依赖的话,当在配置类中
   使用@ConfigurationProperties(prefix = "xxx")注解时,
   我这个版本的spring boot会提示有问题
-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artif
    <optional>true</optional>
</dependency>

解决方法2:
在这里插入图片描述

java代码:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "user")
@Component
public class User {
	// 有了前缀,可以省去@Value注解
    private String username;//csp789
    private String password;//456
    private int age;//18
}

@ConfigurationProperties 和 @EnableConfigurationProperties 配合使用

单独使用@ConfigurationProperties时

@Data
@Component // 这时候加该注解 将DemoConfig 交给IOC管理
@ConfigurationProperties(prefix = "csp.user")
public class DemoConfig {
	private String username;
	private String password;
}

这种情况下,需要使用 DemoConfig时,直接:

@Autowired
private DemoConfig demoConfig;

即可!

@ConfigurationProperties 和 @EnableConfigurationProperties 配合使用

@Data
//@Component //  未将DemoConfig1 交给IOC管理
@ConfigurationProperties(prefix = "csp.user2")
public class DemoConfig1 {
	private String username1;
	private String password1;
}

@Data
//@Component //  未将DemoConfig2 交给IOC管理
@ConfigurationProperties2(prefix = "csp.user2")
public class DemoConfig2 {
	private String username2;
	private String password2;
}

这种情况下,需要使用 DemoConfig1和DemoConfig2

@Service // 或者@Configuration/@Componet/@Controller/@Repository 只需要交给IOC管理即可
@EnableConfigurationProperties({
        DemoConfig1.class,
        DemoConfig2.class
})
public class TestService{
	@Autowired
	private DemoConfig1 demoConfig1;

	@Autowired
	private DemoConfig2 demoConfig2;
}

@PropertySource和@ConfigurationProperties配合使用

@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "user")
@Component
@PropertySource(value={"classpath:/user.properties"})
public class User {
    private String username;//csp1999
    private String password;//123456
    private int age;//22
}

如果文章对您有帮助,还请您一键三连!