Create an @ConfigurationProperties class and specify the profile using the @Profile annotation
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@Profile("dev")
@ConfigurationProperties(prefix = "user")
public class DevUserProperties {
private Map<String, String> user;
public Map<String, String> getUser() {
return user;
}
public void setUser(Map<String, String> user) {
this.user = user;
}
}
Create another one
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@Profile("local")
@ConfigurationProperties(prefix = "user")
public class LocalUserProperties {
private Map<String, String> user;
public Map<String, String> getUser() {
return user;
}
public void setUser(Map<String, String> user) {
this.user = user;
}
}
In your service, inject the @ConfigurationProperties class according to the active profile
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class UserService {
private final DevUserProperties devUserProperties;
private final LocalUserProperties localUserProperties;
@Autowired
public UserService(DevUserProperties devUserProperties, LocalUserProperties localUserProperties) {
this.devUserProperties = devUserProperties;
this.localUserProperties = localUserProperties;
}
public Map<String, String> getUserProperties() {
if (devUserProperties != null && devUserProperties.getUser() != null) {
return devUserProperties.getUser();
} else if (localUserProperties != null && localUserProperties.getUser() != null) {
return localUserProperties.getUser();
}
return null;
}
}
In this way, you can activate multiple profiles and only get properties from a particular profile.