Home > database >  Spring boot: Point to different properties file from my main properties file
Spring boot: Point to different properties file from my main properties file

Time:06-16

How can I point to different properties file from my main properties file depending on needs?

My main file contains different webservices types and where to find the specific file to each of them, where I will be able to access more information about that particular service.

I have a main properties file

webservices.properties:

webs1=C:\Users\yyy\webs1.properties
webs2=C:\Users\yyy\webs2.properties
webs3=C:\Users\yyy\webs3.properties
webs4=C:\Users\yyy\webs4.properties

inside the web services properties file

webs1.properties:

key1=value1
key2=value2
key3=value3

Currently my application only uses one Webservice. I call its properties file with @PropertySource in the Config class. And then use the Environment.getProperty() method whenever I want to access one of the values.

How do I make it as such I call the specific properties file of the Webservice that I'm currently using?

Config.class

@PropertySource(value = "file:src/main/resources/sddv1.properties", name = "sample.props")
    @Configuration
    public class Config {
    
        @Autowired
        Environment env;
        
        @Bean
        public void doStuff() {
            String prop1 = env.getProperty(key1);
            // I want to loop through different webservices and use the method above on each of their properties files
        }
    }

CodePudding user response:

Use the standard application-properties or use profiles.

https://docs.spring.io/spring-boot/docs/2.6.8/reference/htmlsingle/#features.profiles

Or just override the defined properties with environment variables.

https://docs.spring.io/spring-boot/docs/2.6.8/reference/htmlsingle/#features.external-config

  • Related