Ravi
Ravi

Reputation: 7

Spring Boot external configuration using @PropertySource annotation with file

I am new to spring boot and I want to read properties from external file using place holder @PropertySource("file:${application_properties}"), so used annotation in configuration files as mentioned and defined @Bean,

public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

But I am getting:

Could not resolve placeholder 'application_properties' in value "file:${application_properties}"

I configured applications_properties in tomcat server.xml/context.xml (not sure about which is file exactly)

Note: Its working fine if I use @PropertySource("file:c:\filename") but I want to use placeholder and want to define that place holder in tomcat (Want to know how to do that as well). Could you please help me to read the file properties using @PropertySource which will read the place holder defined in tomcat.

Thanks

Upvotes: 1

Views: 3124

Answers (2)

Thirumal
Thirumal

Reputation: 9536

The below class file has two properties files sql.properties & errorDefinition.properties files (/src/main/resource)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySources({
        @PropertySource("classpath:sql.properties"),
        @PropertySource("classpath:errorDefinition.properties")
})
public class PropConfig {

    public PropConfig() {
        super();
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer PropertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

Upvotes: 1

Alien
Alien

Reputation: 15878

You can use like below.

@PropertySource("file:${app.home}/app.properties")

and pass during startup like below.

System.setProperty("app.home", "test");

java -jar -Dapp.home="/home/mkyon/test" example.jar

see this

Upvotes: 0

Related Questions