标签: properties-file

使用Spring从Properties文件加载嵌套占位符

是否可以从属性文件中加载嵌套占位符?我正在尝试动态加载URL.

例如,如果我的属性文件包含

my.url=http://localhost:8888/service/{nestedProperty}/
Run Code Online (Sandbox Code Playgroud)

有没有办法在运行时加载{nestedProperty}的值?与ResourceBundle的行为类似.如果是这样,我将如何有效地实例化String?到目前为止,我在想

<bean id="myURLString" class="java.lang.String" scope="prototype" lazy-init="true">
    <property name="URL" value="${my.url}" />
</bean>
Run Code Online (Sandbox Code Playgroud)

...但我不确定要嵌套什么属性.如果可能的话,我想使用Annotations获取bean,尽管我目前有一些东西

ctx.getBean("myURLString", String.class, new Object[] { nestedProperty} );
Run Code Online (Sandbox Code Playgroud)

我在这里查看了PropertyPlaceholderConfigurer和其他几个属性文件问题,但我似乎无法弄清楚这是否可行.

我还应该注意,我想从我的代码中动态加载这个嵌套属性,或者至少从那里操作它们(可能通过@PostConstruct?)

java spring properties-file

7
推荐指数
1
解决办法
4533
查看次数

如何将新数据附加到属性文件中的现有数据?

我使用以下代码将数据写入属性文件

public void WritePropertiesFile(String key, String data)
{
Properties configProperty = new Properties();
configProperty.setProperty(key, data);
File file = new File("D:\\Helper.properties");
FileOutputStream fileOut = new FileOutputStream(file,true);
configProperty.store(fileOut, "sample properties");
fileOut.close();
}

I am calling the above method 3 times as follows:
help.WritePropertiesFile("appwrite1","write1");
help.WritePropertiesFile("appwrite2","write2");
help.WritePropertiesFile("appwrite3","write3");
Run Code Online (Sandbox Code Playgroud)

但是,Helper.properties文件中的数据显示如下:

#sample properties
#Mon Jul 01 15:01:45 IST 2013
appwrite1=write1
#sample properties
#Mon Jul 01 15:01:45 IST 2013
appwrite2=write2
appwrite1=write1
#sample properties
#Mon Jul 01 15:01:45 IST 2013
appwrite3=write3
appwrite2=write2
appwrite1=write1
Run Code Online (Sandbox Code Playgroud)

我希望数据附加到现有数据,并且不需要重复数据,如下所示:

appwrite3=write3
appwrite2=write2
appwrite1=write1
Run Code Online (Sandbox Code Playgroud)

请建议怎么做?

java properties-file

7
推荐指数
1
解决办法
2万
查看次数

从属性文件中删除条目

如何从属性文件中删除键和值?我的属性文件包含以下内容:

key1=value1 
key2=value2
Run Code Online (Sandbox Code Playgroud)

我使用下面的代码删除条目key2=value2.之后,现在该文件具有以下值:

key1=value1 
key2=value2
Wed Mar 06 12:36:32 IST 2013 
key1=value1
Run Code Online (Sandbox Code Playgroud)

用于删除条目的java代码:

FileOutputStream out1 = new FileOutputStream(file, true);
prop.remove(key);
prop.store(out1,null);
Run Code Online (Sandbox Code Playgroud)

我在做什么错误.如何在编写之前清除文件的全部内容.

java properties properties-file

6
推荐指数
1
解决办法
2077
查看次数

使IntelliJ知道属性文件

有没有办法告诉IntelliJ特定.properties文件将被加载到项目的环境中?我们使用@PropertySource注释来加载属性文件,并在几个地方加载由已配置属性确定的文件中的覆盖值,如下所示:

@Configuration
@PropertySource("classpath:com/example/package/db.properties")
@PropertySource("classpath:com/example/package/db.${db.instance}.properties")
public class DatabaseConfig {
    private @Value("${db.someProperty}") String someDBProperty;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

问题是在其中一个间接引用的文件中,例如db.test.properties,IntelliJ不知道是否/如何引用属性文件,因此它不能将任何条目绑定到它们的用法.(奇怪的是,这些文件中列出的某些属性并不是灰色的,表示"未使用的属性",但即使这些属性也没有为"查找用法"搜索提供任何结果).直接命名的文件没有问题,例如db.properties上面.

有没有办法告诉IntelliJ这些文件是否创建@Configuration了引用它们的额外虚拟文件?

java spring intellij-idea properties-file

6
推荐指数
1
解决办法
7136
查看次数

Apache Commons 配置验证属性文件

我正在使用Apache Commons Configuration库与PropertiesConfiguration. 我的应用程序在启动后立即加载配置文件,如下所示:

public PropertiesConfiguration loadConfigFile(File configFile) throws ConfigurationNotFoundException {
        try {
            if (configFile != null && configFile.exists()) {
                config.load(configFile);
                config.setListDelimiter(';');
                config.setAutoSave(true);
                config.setReloadingStrategy(new FileChangedReloadingStrategy());
                setConfigLoaded(true);
            }
            else {
                throw new ConfigurationNotFoundException("Configuration file not found.");
            }

        } catch (ConfigurationException e) {
            logger.warn(e.getMessage());
            setDefaultConfigValues(config);
            config.setFile(configFile);
        }
        return config;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何验证configFile,以便我可以确定该文件中没有丢失任何属性,并且稍后在我的代码中NullPointerException尝试访问属性时我不会得到 a ,例如:

PropertiesConfiguration config = loadConfig(configFile);
String rootDir = config.getString("paths.download"); // I want to be sure that this property exists right at the …
Run Code Online (Sandbox Code Playgroud)

java validation properties-file apache-commons-config

6
推荐指数
1
解决办法
1720
查看次数

是否有与 Lookups 等效的功能,但在目标主机而不是本地执行?

在 Ansible 中,当我需要从 Java 属性文件 ( ) 读取属性时.properties,我会执行以下操作:

- name: Read properties
  set_fact:
    myProp1: {{ lookup('ini', 'myProp1 type=properties file=/path/to/file.properties }} 
    myProp2: {{ lookup('ini', 'myProp2 type=properties file=/path/to/file.properties }}
Run Code Online (Sandbox Code Playgroud)

但是,正如Ansible 文档所述:

查找发生在本地计算机上,而不是远程计算机上。

如果属性文件位于远程目标主机,我该怎么办?我无法使用 include_vars,因为我的属性文件具有 Java 属性文件格式。

properties-file ansible

6
推荐指数
1
解决办法
2313
查看次数

Spring cloud config - 加载其他文件

我为我的应用程序安装了一个spring cloud服务器,在启动时提供公共属性文件.

"正常"功能正常运行.应用程序获取文件层次结构并获得对加载参数的完全访问权限.

Application.properties
Application-<profile>.properties
<applicationname>.properties
...
Run Code Online (Sandbox Code Playgroud)

现在的问题是:如何在通用命名旁边加载配置文件?

如果我在repositoy中存储另一个文件,例如sharedCommonServers.properties,如何使用配置服务器另外加载此文件?此文件包含某些但不是所有程序使用的属性.

先感谢您!

亨德里克

java spring properties-file spring-cloud-config

6
推荐指数
1
解决办法
1003
查看次数

Spring Boot:有没有办法防止某些环境属性被覆盖?

我正在使用 Spring Boot 及其外部配置功能: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

在我的应用程序中,我有一个application.properties具有默认配置的文件,例如:

my.property=one
other.property=two
Run Code Online (Sandbox Code Playgroud)

在生产和测试环境中,我使用 Spring Boot 的外部配置功能来覆盖某些属性。据我了解,这种配置概念贯穿整个Spring Boot,几乎任何选项都可以在外部配置。

这引出了我的问题:

  • 如果我有一些属性,但我不想公开这些属性以进行覆盖,该怎么办?
  • 是否有某种“黑名单”选项来防止所选属性列表被覆盖?

java configuration spring properties-file spring-boot

6
推荐指数
0
解决办法
706
查看次数

Spring boot - 无法设置默认语言环境

我现在正在开发 Spring Boot 应用程序(仅休息控制器)。对我的服务器的每个请求都包含语言标签。我想根据此标签以特定语言发送响应。控制器从请求中提取这些标签并作为 Locale 实例提供给服务层。我在 src/main/resources 下创建了属性文件,如下所示:https : //i.stack.imgur.com/fsXPG.jpg

我的问题是默认语言。无论提供何种语言环境,从属性文件返回的消息始终来自 AppResources_pl.properties。我认为,问题在于默认语言环境。现在我尝试了两种不同的方法,但仍然不知道如何将默认属性文件设置为英文。

第一的:

ResourceBundle myResources = ResourceBundle.getBundle("AppResources", currentLocale);
String message = myResources.getString("label.error");
Run Code Online (Sandbox Code Playgroud)

第二:

@Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.forLanguageTag("en-US"));
        return slr;
    }

    @Bean
    public MessageSource messageSource(){
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("AppResources");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
Run Code Online (Sandbox Code Playgroud)

并在服务中:

messageSource.getMessage("label.error", null, locale)
Run Code Online (Sandbox Code Playgroud)

双向返回消息,但总是来自 AppResources_pl.properties。当在 *.properties 文件下找不到提供的语言环境时,如何将 AppResources.properties 设置为默认属性文件?

java locale properties-file spring-boot

6
推荐指数
2
解决办法
2万
查看次数

原始类型的属性不允许使用“lateinit”修饰符 - Kotlin

我在尝试将环境变量值分配给变量时遇到错误lateinit。错误是“基本类型的属性上不允许使用'lateinit'修饰符”

我的application.properties(读取环境变量)

my.property.from.properties.file=true
Run Code Online (Sandbox Code Playgroud)

我的服务类:

@Component
class MyService @Autowired constructor(
    private val someService: SomeService) {

    @Value("\${my.property.from.properties.file}")
    private lateinit var myBooleanEnabled: Boolean
Run Code Online (Sandbox Code Playgroud)

给它赋值并不能解决问题。例如,与

private lateinit var myBooleanEnabled: Boolean = true
Run Code Online (Sandbox Code Playgroud)

我收到 2 个错误:

  • 原始类型的属性不允许使用“lateinit”修饰符
  • 带有初始值设定项的属性不允许使用“lateinit”修饰符

根据我读到的内容,我需要一个委托(https://kotlinlang.org/docs/reference/deleated-properties.html),但我无法完全掌握它。另外,如果有“更干净”的解决方案,我不想编写另一个方法来设置属性。有任何想法吗?

spring properties-file kotlin

6
推荐指数
1
解决办法
8962
查看次数