6. Get Active Profiles
We have two ways to do it, using Environment
or spring.profiles.active
.
# 方式1:Using Environment
public class ProfileManager {
@Autowired
private Environment environment;
public void getActiveProfiles() {
for (String profileName : environment.getActiveProfiles()) {
System.out.println("Currently active profile - " + profileName);
}
}
}
# 方式2:Using spring.profiles.active
# If there are several, activeProfiles contain their names separated by a comma
# if no profiles are active, activeProfiles is an empty string
public class ProfileManager {
@Value("${spring.profiles.active:}")
private String activeProfiles;
public String getActiveProfiles() {
for (String profileName : activeProfiles.split(",")) {
System.out.println("Currently active profile - " + profileName);
}
}
}