lodsve.core.properties.profile.ProfileInitializer Maven / Gradle / Ivy
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package lodsve.core.properties.profile;
import lodsve.core.properties.Env;
import lodsve.core.properties.env.Configuration;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.CollectionUtils;
import java.util.Set;
/**
* 读取配置的profile.
*
* @author sunhao([email protected])
* @version V1.0, 16/1/30 下午3:20
*/
public class ProfileInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment configEnv = applicationContext.getEnvironment();
Configuration profileConfig = Env.getFrameworkEnv("profiles.properties");
Set profiles = profileConfig.subset("profiles").getKeys();
if (CollectionUtils.isEmpty(profiles)) {
return;
}
for (String profile : profiles) {
if (profileConfig.getBoolean("profiles." + profile, false)) {
configEnv.addActiveProfile(profile);
}
}
}
}