Dhruv
Dhruv

Reputation: 10693

how spring property resolver works?

I have property place holder in my spring context.xml file

<bean id="propertyConfigurer" class="com.techpleiad.poc.RMCPropertyUtil">
        <property name="basenames" value="file:${config.file.dir}/prop_application" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="cacheSeconds" value="30"></property>
    </bean>

and this property 'config.file.dir' is not getting resolved.

'config.file.dir' is the environment variable and when i debug the code and check for the basename the file path comes as it is.. '{config.file.dir}/prop_application'

I need to know what spring code/classes are involved in resolving such properties. How i could debug and resolve this problem?

Upvotes: 1

Views: 6397

Answers (2)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279970

You'll need to register a PropertySourcesPlaceholderConfigurer with a reference to your property sources (or not since this is an environment property which are implicitly added).

With XML you can do that with

<context:property-placeholder location="classpath:spring.properties" />

With Java config, simply define a static @Bean annotated method which returns a PropertySourcesPlaceholderConfigurer.

Upvotes: 4

Braj
Braj

Reputation: 46841

You can try with Spring SpEL to get the system properties

#{systemProperties['config.file.dir']}

To read environment variable use

#{systemEnvironment['config.file.dir']}

The systemEnvironment property contains all the environment variables on the machine where the program is running. Meanwhile, the systemProperties contains all the properties that we set in Java when the application started, using the -D argument.

Upvotes: 0

Related Questions