spring取得プロファイル値ツール類
3188 ワード
springはプロファイル値ツール類を取得し、直接コードを付けます.
public interface IPropertyConfig {
String getProperty(String key);
}
import com.zhaogang.config.IPropertyConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
/**
* @author he.feng
* @date 2016/1/4
*/
@PropertySources(value = {@PropertySource("classpath:/application.properties")})
@Component("webCommonConfig")
public class WebCommonConfig implements IPropertyConfig {
@Resource
Environment env;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@Override
public String getProperty(String key) {
if (null == key) {
return null;
}
if (env.getProperty(key) == null) {
return null;
}
try {
return new String(env.getProperty(key).getBytes("iso-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
return env.getProperty(key);
}
}
}