简体   繁体   中英

What is the Best way to get the values from the Properties File in Spring?

I have used the following ways to get the values from the properties. But I would like to know which one of these is the best to use to follow the coding standard? Also, are there any other ways that we can get the values from the properties file in Spring?

PropertySourcesPlaceholderConfigurer 
getEnvironment() from the Spring's Application Context
Spring EL @Value

Along with the other configuration classes (ApplicationConfiguration etc.) I create a class with the annotation @Service and here I have the following fields to access the properties in my file:

@Service
public class Properties (){

    @Value("${com.something.user.property}")
    private String property;

    public String getProperty (){ return this.property; }

}

Then I can autowire the class and get the properties from my properties file

The answer is, it depends.

If the properties are configuration values, then configure a propertyConfigurer (below is an example for a Spring xml configuration file).

<bean id="propertyConfigurer"
      class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:configuration.properties</value>
            <value>classpath:configuration.overrides.properties</value>
        </list>
    </property>
</bean>

When configured this way, the properties from the last file found override those found earler (in the locations list). This allows you to ship the standard configuration.properties file bundled in the war file and store a configuration.overrides.properties at each installation location to account for installation system differences.

Once you have a propertyConfigurer, annotate your classes using the @Value annotation. Here is an example:

@Value("${some.configuration.value}")
private String someConfigurationValue;

It is not required to cluster the configuration values into one class, but doing so makes it easier to find where the values are used.

@Value will be the simple and easy way to use, as it will inject value from property file to your field.

Both the older PropertyPlaceholderConfigurer and the new PropertySourcesPlaceholderConfigurer added in Spring 3.1 resolve ${…} placeholders within bean definition property values and @Value annotations.

unlike getEnvironment

using property-placeholder will not expose the properties to the Spring Environment – this means that retrieving the value like this will not work – it will return null

when you are using <context:property-placeholder location="classpath:foo.properties" /> and you use env.getProperty(key); it will always return null.

see this post for the problem using getEnvironment : Expose <property-placeholder> properties to the Spring Environment

Moreover, in Spring Boot you can use @ConfigurationProperties to define your own properties with hierarchical and type-safe in application.properties. and you don't need to put @Value for every field.

@ConfigurationProperties(prefix = "database")
public class Database {
    String url;
    String username;
    String password;

    // standard getters and setters
}

in application.properties:

database.url=jdbc:postgresql:/localhost:5432/instance
database.username=foo
database.password=bar

Quote from : properties with spring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM