开发者

how to read properties file?

开发者 https://www.devze.com 2023-02-19 04:26 出处:网络
I am using CXF to create a java web service. i have a file path, e.g \"C:\\ftproot\", ne开发者_开发问答ed to be configurable

I am using CXF to create a java web service.

i have a file path, e.g "C:\ftproot", ne开发者_开发问答ed to be configurable so i want to put this path into a properties file e.g application.properties

but how can i read the properties file in my java code?

can anyone help?

Thanks


You need to put the properties file in your resources or WEB-INF/classes

Then

Properties properties = new Properties();
try {
    properties.load(new FileInputStream("classpath:application.properties"));
} catch (IOException e) {
}

See Also

  • Properties


Create a PropertyPlaceholderConfigurer for Spring (Refer to the API for options).

Example:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="searchSystemEnvironment" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:build.properties</value>
            <value>classpath:other.properties</value>
        </list>
    </property>
</bean>

Assuming you have a property file.path in the property file and you are using component scanning you can then use:

@Value("file.path") private String filePath;

That will then be populated with the value of file.path in the property file (if the bean is created by Spring)

Or if you are creating your beans in XML:

<bean class="yourClassName">
    <property name="filePath" value="${file.path} />
</bean>
0

精彩评论

暂无评论...
验证码 换一张
取 消