下面列出了org.springframework.core.io.support.PropertiesLoaderUtils#loadProperties ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void createIndexes(final String domain, final DataSource dataSource) throws IOException {
LOG.debug("[{}] Creating indexes", domain);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
Properties indexes = PropertiesLoaderUtils.loadProperties(indexesXML.getResource());
indexes.stringPropertyNames().stream().sorted().forEachOrdered(idx -> {
LOG.debug("[{}] Creating index {}", domain, indexes.get(idx).toString());
try {
jdbcTemplate.execute(indexes.getProperty(idx));
} catch (DataAccessException e) {
LOG.error("[{}] Could not create index", domain, e);
}
});
LOG.debug("Indexes created");
}
/**
* 配置文件信息获取
*
* @return
*/
@Override
public Map<String, Object> defaultConfig() {
Map<String, Object> rtnMap = new HashMap<>(1);
try {
ClassLoader classLoader = this.getClass().getClassLoader();
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(getPropFilePath()) :
ClassLoader.getSystemResources(getPropFilePath()));
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
rtnMap.put((String) entry.getKey(), entry.getValue());
}
}
} catch (IOException e) {
log.error("加载初始配置错误.", e);
}
return rtnMap;
}
public static Properties loadI18nProp(){
if (prop != null) {
return prop;
}
try {
// build i18n prop
String i18nFile = "i18n/message.properties";
// load prop
Resource resource = new ClassPathResource(i18nFile);
EncodedResource encodedResource = new EncodedResource(resource,"UTF-8");
prop = PropertiesLoaderUtils.loadProperties(encodedResource);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return prop;
}
public static Properties loadI18nProp() {
if (prop != null) {
return prop;
}
try {
// build i18n prop
String i18n = XxlJobAdminConfig.getAdminConfig().getI18n();
i18n = StringUtils.isNotBlank(i18n) ? ("_" + i18n) : i18n;
String i18nFile = MessageFormat.format("i18n/message{0}.properties", i18n);
// load prop
Resource resource = new ClassPathResource(i18nFile);
EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
prop = PropertiesLoaderUtils.loadProperties(encodedResource);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return prop;
}
static Collection<BinderType> parseBinderConfigurations(ClassLoader classLoader,
Resource resource) throws IOException, ClassNotFoundException {
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
Collection<BinderType> parsedBinderConfigurations = new ArrayList<>();
for (Map.Entry<?, ?> entry : properties.entrySet()) {
String binderType = (String) entry.getKey();
String[] binderConfigurationClassNames = StringUtils
.commaDelimitedListToStringArray((String) entry.getValue());
Class<?>[] binderConfigurationClasses = new Class[binderConfigurationClassNames.length];
int i = 0;
for (String binderConfigurationClassName : binderConfigurationClassNames) {
binderConfigurationClasses[i++] = ClassUtils
.forName(binderConfigurationClassName, classLoader);
}
parsedBinderConfigurations
.add(new BinderType(binderType, binderConfigurationClasses));
}
return parsedBinderConfigurations;
}
/**
* 获取properties文件内容
*
* @param propertiesPath :绝对路径
* @return
*/
public static Properties getProperties(String propertiesPath) throws IOException {
Properties resultProperties = propertiesMap.get(propertiesPath);
if (resultProperties == null) {
Resource resource = new PathResource(propertiesPath);
try {
resultProperties = PropertiesLoaderUtils.loadProperties(resource);
} catch (FileNotFoundException e) {
resultProperties = null;
}
if (resultProperties != null)
propertiesMap.put(propertiesPath, resultProperties);
}
return resultProperties;
}
@Bean
public AwesomeURL awesomeUrl() throws IOException {
String configPath = storage+".properties";
Resource resource = new ClassPathResource(configPath);
Properties props = PropertiesLoaderUtils.loadProperties(resource);
Set<String> keySet = props.stringPropertyNames();
String host = props.getProperty("host");
int port = Integer.parseInt(props.getProperty("port"));
String username = props.getProperty("username");
String password = props.getProperty("password");
Map<String,String> paramMap = new HashMap<>();
for (String key:keySet){
paramMap.put(key, (String) props.get(key));
}
AwesomeURL awesomeURL = new AwesomeURL(storage,username,password,host,port,null,paramMap);
return awesomeURL;
}
public static Properties loadI18nProp(){
if (prop != null) {
return prop;
}
try {
// build i18n prop
String i18n = XxlJobAdminConfig.getAdminConfig().getI18n();
i18n = StringUtils.isNotBlank(i18n)?("_"+i18n):i18n;
String i18nFile = MessageFormat.format("i18n/message{0}.properties", i18n);
// load prop
Resource resource = new ClassPathResource(i18nFile);
EncodedResource encodedResource = new EncodedResource(resource,"UTF-8");
prop = PropertiesLoaderUtils.loadProperties(encodedResource);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return prop;
}
public static Properties loadI18nProp() {
if (prop != null) {
return prop;
}
try {
// build i18n prop
String i18n = XxlJobAdminConfig.getAdminConfig().getI18n();
i18n = StringUtils.isNotBlank(i18n) ? ("_" + i18n) : i18n;
String i18nFile = MessageFormat.format("i18n/message{0}.properties", i18n);
// load prop
Resource resource = new ClassPathResource(i18nFile);
EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
prop = PropertiesLoaderUtils.loadProperties(encodedResource);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return prop;
}
public static String getString(String key) {
Properties prop = null;
try {
Resource resource = new ClassPathResource(file_name);
EncodedResource encodedResource = new EncodedResource(resource,"UTF-8");
prop = PropertiesLoaderUtils.loadProperties(encodedResource);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
if (prop!=null) {
return prop.getProperty(key);
}
return null;
}
public ApplicationProperties() {
try {
Resource resource = new ClassPathResource("/application.properties");//
properties = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void customize(ConfigurableEnvironment env) {
try {
Properties props;
ClassPathResource resource = new ClassPathResource(DEFAULT_PROPERTY);
props = PropertiesLoaderUtils.loadProperties(resource);
props.put(SPRINGBOOT_MANAGEMENT_PORT_KEY, getManagementPort(env));
env.getPropertySources().addLast(new PropertiesPropertySource("managementProperties", props));
} catch (IOException e) {
logger.error("Failed to load " + DEFAULT_PROPERTY);
}
}
@Override
public void customize(ConfigurableEnvironment env) {
try {
Properties props;
ClassPathResource resource = new ClassPathResource(DEFAULT_PROPERTY);
props = PropertiesLoaderUtils.loadProperties(resource);
props.put(SPRINGBOOT_MANAGEMENT_PORT_KEY, getManagementPort(env));
env.getPropertySources().addLast(new PropertiesPropertySource("managementProperties", props));
} catch (IOException e) {
logger.error("Failed to load " + DEFAULT_PROPERTY);
}
}
private Properties loadProperties(String name, Resource resource) throws IOException{
Properties properties = null;
if(name.contains("properties")){
properties = PropertiesLoaderUtils.loadProperties(resource);
}else{
YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
bean.setResources(resource);
properties = bean.getObject();
}
return properties;
}
/**
* Reads default Citrus project property file for active project.
* @return properties loaded or empty properties if nothing is found
*/
public Properties getProjectProperties() {
File projectProperties = fileBrowserService.findFileInPath(new File(project.getProjectHome()), "citrus.properties", true);
try {
if (projectProperties != null) {
return PropertiesLoaderUtils.loadProperties(new FileSystemResource(projectProperties));
}
} catch (IOException e) {
log.warn("Unable to read default Citrus project properties from file resource", e);
}
return new Properties();
}
/**
* 获取配置文件
*
* @return 配置Props
*/
private static Properties getProperties() {
// 读取配置文件
Resource resource = new ClassPathResource("/config/application.properties");
Properties props = new Properties();
try {
props = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
e.printStackTrace();
}
return props;
}
/**
* 从安装文件加载配置
*
* @return
* @see #INSTALL_FILE
*/
private Properties fromInstallFile() {
File file = SysConfiguration.getFileOfData(INSTALL_FILE);
if (file.exists()) {
try {
return PropertiesLoaderUtils.loadProperties(new FileSystemResource(file));
} catch (IOException e) {
throw new SetupException(e);
}
}
return new Properties();
}
/**
* 加载文件
* @param location
* @param filename
* @param
*/
private Properties load(String location,String filename) throws Exception{
Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource(location+filename));
return properties;
}
/**
* 加载文件
* @param location
* @param filename
* @param
*/
private static Properties load(String location,String filename) throws Exception{
Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource(location+filename));
return properties;
}
/**
* 加载文件
* @param location
* @param filename
* @param
*/
private static Properties load(String location,String filename) throws Exception{
Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource(location+filename));
return properties;
}