spring配置spring.profiles.active=dev 加载配置信息,使用@PropertySource加载prod内容发现加载不了

spring配置spring.profiles.active=dev 加载配置信息,而我想使用@PropertySource(value = {"classpath:application-prod.properties"})加载prod的内容发现加载的数据仍然是dev环境的配置信息,是加载不了吗?网上找不到博客

img

ReadProperties4

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @Author 211145187
 * @Date 2022/7/20 15:51
 **/
@ConfigurationProperties(prefix = "logging.level")//这个注解是用找到类    注意:@ConfigurationProperties无法加载自定义配置问价内容,必须和@PropertySource配合使用才能获取
@Component  //生效的两种方式:方式1:配置@Component,方式2:启动类添加@EnableConfigurationProperties(ReadProperties.class)
@PropertySource(value = {"classpath:application-prod.properties"})
@Data
public class ReadProperties4 {
    private String root;
}

application.properties

server.port=8080
spring.profiles.active=dev 

application-dev.properties

logging.level.root = info

application-prod.properties

logging.level.root = warn

最后打印readProperties4结果:仍显示dev环境的info,而不是显示prod环境的warn

img


img


先后顺序罢了,前面配置文件取到数据,就直接返回了

朋友,启动类没有传入参数 args

application-prod.properties已经加载了 只不过它的优先级最低

img

server.port=8080
spring.profiles.active=dev 

这个修改为pro,重新启动项目试一下。