Is there a way to get list of loaded properties file in SpringBoot application?
Asked Answered
N

3

9

I'm facing an issue on only one platform when I'm trying to execute mvn clean install. As part of the build we compile multiple component and last we execute functional testing using wiremock. It is supposed to pick specific configuration from function testing profile and default properties should be picked from application.properties file. But for some reason same code isn't able to find the properties mentioned in these file. So, just wondering if somehow, if I can get the list of properties files being loaded during wiremock ? This will give some clue on why isn't expected properties files are being picked ?

All properties files are located inside :

src/main/resources

And, following from test class.

@ContextConfiguration(classes = SampleFTConfiguration.class)
public class SampleControllerTest{
//test method
}

@ComponentScan("com.xxx.xxx.xxx.ft")
@PropertySource("classpath:application-test.properties")
public class  SampleFTConfiguration{


}

Note : I'm not expecting anyone to fix the issue, all I wanted to know, if we can get the name of loaded property files ?

Nildanile answered 22/3, 2021 at 6:32 Comment(4)
Please add a code of your test, at least the annotations you're using in the test as well as the exact location of application.properties file. Without the code we can only speculateWenz
You can get these details in log file/console. At server start up, all the details about configuration files being loaded are logged in log file/console.Tenement
@Tenement Nope, we don't get file name in log/consoleNildanile
If you enable the DEBUG log, it should show the properties file loaded. I just created sample spring boot application and started with DEBUG logging, here is what I see in logs 2021-03-23 11:53:17.157 DEBUG 16544 --- [ main] o.s.w.c.s.StandardServletEnvironment : Adding [applicationConfig: [classpath:/application.properties]] PropertySource with search precedence immediately lower than [applicationConfigurationProperties] Tenement
S
6

To troubleshoot context configuration on app startup (for those who don't have an access to app sources) you can add

logging:
  level:
    org.springframework.boot.context.config: trace

to your application.yml to get filenames:

2022-10-26 13:29:45.977 TRACE [,,] 16522 --- [main] o.s.b.context.config.ConfigDataLoaders   : Loading file [config/application-app.
yml] using loader org.springframework.boot.context.config.StandardConfigDataLoader
2022-10-26 13:29:45.977 TRACE [,,] 16522 --- [main] o.s.b.context.config.ConfigDataLoaders   : Loading file [config/application-ppe.
yml] using loader org.springframework.boot.context.config.StandardConfigDataLoader

Adding org.springframework.boot.context.properties: trace doesn't help much though. Some user-defined properties get logged, however others don't.

Serrulate answered 26/10, 2022 at 14:4 Comment(1)
In my opinion, this solution answers the title question Is there a way to get list of loaded properties file in SpringBoot application? best.Bussell
Z
4

After searching and trying out for a while, looks like ConfigurableEnvironment is what you're trying to find.

The code is pretty simple. However I think it's better to debug and check the configurableEnvironment value directly, so you can adjust the code to your needs (remove filter name, etc).

  @Autowired
  private ConfigurableEnvironment configurableEnvironment;

  @Test
  public void getProperties() {
    Map<String, Object> mapOfProperties = configurableEnvironment.getPropertySources()
        .stream()
        .filter(propertySource -> propertySource.getName()
            .contains("application-test.properties"))
        .collect(Collectors.toMap(PropertySource::getName, PropertySource::getSource));
    mapOfProperties.values()
        .forEach(System.out::println);
  }

the code will printed out

{properties-one=value-for-properties-one, properties-two=value-for-properties-two}

with my application-test.properties value

properties-one=value-for-properties-one
properties-two=value-for-properties-two

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/ConfigurableEnvironment.html

Zandra answered 26/3, 2021 at 6:19 Comment(0)
W
1

Ok, following the test definition please make sure that:

  1. You should run the test with spring runner (spring extension if you're on JUnit5). So you should place the annotation @RunWith(SpringRunner.class) (or @ExtendsWith(SpringExtension.class) for junit 5)

  2. The property source you're using is application-test.properties. You've said that the properties file is located in src/main/resources but the file name probably implies that it should reside in src/test/resources

Wenz answered 22/3, 2021 at 6:54 Comment(3)
Sorry, application-test.properties is inside src/test/resources. But, even I add any properties inside that file.. it doesn't picked up inside application. I do not want to make any changes to the code, because I know there is no issue with code. It is something to with platform where for some reason correct properties files are not getting loaded.Nildanile
So are you using SpringRunner/SpringExtension? Without it the whole spring infrastructure won't startWenz
I guess it should be @ExtendWith and not @ExtendsWithCookshop

© 2022 - 2024 — McMap. All rights reserved.