ishan3243
ishan3243

Reputation: 1928

Accessing user defined properties using spring expression language

Is it poosible to read user defined .properties file using SpEL? I know we can do something like this for systemProperties

  @Value("#{ systemProperties['user.region'] }")

I want to access a property in a user defined properties file. Also is it possible to use SpEL in @ContextConfiguration annotation? I want to set the value of this annotation using a properties file defined by me.

Upvotes: 0

Views: 1165

Answers (2)

Gary Russell
Gary Russell

Reputation: 174514

Yes, you can use...

@Bean
public Properties props() {
    ...
}

@Value("#{props.foo}")

How do you want to use SpEL in @ContextConfiguration ?

Upvotes: 1

Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22506

Yes, you can do:

private @Value("${propertyName}") String propertyField;

And you will need PropertyPlaceholderConfigurer

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="classpath:myProps.properties" />

or with java config

@PropertySource("classpath:myProps.properties")

on the config class

Upvotes: 1

Related Questions