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