Mathias
Mathias

Reputation: 3981

How do i access -D parameter from Spring Java Config?

i'm looking into migrating my Spring XML config into Java. I'm having some trouble with my PlaceHolderConfigurer.

In XML i have "locations set up as

<property name="locations">
        <list>
            ...
            <value>file:////${project.home}/conf/jdbc.properties</value>
        </list>
    </property>

, where "project.home" is a parameter i've set with "-Dproject.home=...."

Now, i'm not sure how to do this with Java, since i can't just use

new FileSystemResource("file:////${project.home}/conf/jdbc.properties"),

So, if i want to use PropertySourcesPlaceholderConfigurer.setLocations with a system.property, how do i do that? Pointers appreciated.

Upvotes: 2

Views: 1334

Answers (3)

Gopinath
Gopinath

Reputation: 4957

A combination of context:property-placeholder and @Value annotation can be used for injecting a set of properties into Spring Beans easily.


Here is the 3-step procedure to accomplish this:

Step 1: Define all the required properties inside a 'key=value' type file

application.properties

Step 2: Specify the location of application.properties file in the bean config, using property-placeholder

Step 3: Use @Value annotation in Java program to fetch properties.


Here are the code snippets for a working example:

Step 1: Define properties in 'key=value' format

# File name: application.properties

db.schema=my_schema
db.host=abc.xyz.com:3306
db.table=my_table

Step 2: Mention the location of properties file using property-placeholder

<beans xmlns="http://www.springframework.org/schema/beans" ...>

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

    <!-- other content -->
</beans>

Step 3: Fetch properties using @Value annotation

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;


public class MyProgram {

    @Value("${db.host}")
    private String dbHost;

    @Value("${db.schema}")
    private String dbSchema;

    @Value("${db.table}")
    private String dbTable;

    @Override
    public void showConfig() {
        System.out.println("DB Host = " + dbSchema);
        System.out.println("DB Schema = " + dbSchema);
        System.out.println("DB Table = " + dbSchema);
    }
}

Output of call to showConfig()

DB Host = abc.xyz.com:3306
DB Schema =  my_schema
DB Table = my_table

More information:

https://memorynotfound.com/load-properties-spring-property-placeholder/

https://memorynotfound.com/loading-property-system-property-spring-value/

https://mkyong.com/spring/spring-propertysources-example/

Upvotes: 1

Karol Dowbecki
Karol Dowbecki

Reputation: 44960

Since you are attempting to call PropertySourcesPlaceholderConfigurer.setLocations() with custom locations you probably should use @PropertySource annotation.

@Configuration
@PropertySource(value="file://#{systemProperties['project.home']}/conf/jdbc.properties")
public class MyConfig {

}

You can also configure the PropertySourcesPlaceholderConfigurer bean if you need more fine grained control e.g. if your location uses patterns. See this answer for more details.

Upvotes: 0

j.i.t.h.e.s.h
j.i.t.h.e.s.h

Reputation: 4508

System.getProperty will give you the property set using -D java option

Upvotes: 0

Related Questions