Saturday, August 26, 2017

Spring Boot Quiz 1 – 24.3 Application property file

What follows is a small quiz to determine  if one has understood the loading order of the application.yml file as describe in section 24.3 (Application property files) of the spring boot version 1.5.6.RELEASE documentation (shown below).


The object of the quiz is to determine the output of the HelloController, shown below, given the start command and the working directory.


@RestController
public class HelloController {

    @Value("${location}")
    private String location;

    @RequestMapping("/")
    public String index() {
        return String.format("Greetings from: %s", location);
    }
}


The next picture below shows the structure of the getting started spring boot repository (gs-spring-boot) as cloned from github and its location on my file system.

In various directories throughout the project, application.yml files have been created. In each file, one will find the absolute path of the file as an attribute called “location”.  For example, in the first top most application.yml file shown below in the picture, one will find: location:  
Z:\gs-spring-boot\application.yml


The first questions are based on the above file system structure. The gs-springboot.jar is a fat jar.

1.)

When started as follows:
Z:\>java -jar gs-spring-boot\complete\target\gs-springboot.jar


The loaded application.yml is:
Greetings from: Z:\gs-spring-boot\complete\src\main\resources\config\application.yml

Location described by line 3. 

Remember that the files in src\main\resources are copied into the classpath root of the resulting jar produced in the target directory. Locations 1 and 2 failed therefore 3 was used.


2.)

When started as follows:
Z:\gs-spring-boot>java -jar complete\target\gs-springboot.jar


The loaded application.yml is:
Greetings from: Z:\gs-spring-boot\config\application.yml

Location described by line 1. The config subdirectory  of the current directory.


3.)

When started as follows:
Z:\gs-spring-boot\complete>java  -jar target\gs-springboot.jar 


The loaded application.yml is:
Greetings from: Z:\gs-spring-boot\complete\config\application.yml

Location described by line 1. The config subdirectory  of the current directory.


4.)

When started as follows:
Z:\gs-spring-boot\complete\target>java -jar gs-springboot.jar


The loaded application.yml is:
Greetings from: Z:\gs-spring-boot\complete\src\main\resources\config\application.yml

Location described by line 3. The classpath root config subdirectory.




5.)

When started as follows:
Z:\gs-spring-boot>java -jar complete\target\gs-springboot.jar


The loaded application.yml is:
Greetings from: Z:\gs-spring-boot\application.yml

Location described by line 2. 1 failed there the properties file in current directory was loaded.




6.)

When started as follows:
Z:\gs-spring-boot>java -jar complete\target\gs-springboot.jar


The loaded application.yml is (line 2):
Greetings from: Z:\gs-spring-boot\complete\ src\main\resources\application.yml

Location described by line 4. 1, 2 and 3 failed.




7.)

When started as follows:
Z:\gs-spring-boot>java -jar complete\target\gs-springboot.jar  --spring.profiles.active=dev


The loaded application.yml is:
Greetings from: Z:\gs-spring-boot\config\application-dev.yml

Location described by line 1.


8.)

When started as follows:
Z:\gs-spring-boot\complete>java -jar target\gs-springboot.jar 
                                                             --spring.config.location=file:/gs-spring-boot/custom-config/


The loaded application.yml is:
Greetings from: Z:\gs-spring-boot\custom-config\application.yml


9.)

When started as follows:
Z:\gs-spring-boot\complete>java -jar target\gs-springboot.jar 
                                                             --spring.config.location=custom-config/ 
                                                             --spring.profiles.active=prod


The loaded application.yml is:
Greetings from: Z:\gs-spring-boot\complete\custom-config\application-prod.yml


10.)

When started as follows:
Z:\gs-spring-boot\complete>java -jar target\gs-springboot.jar 
                                                              --spring.config.location=custom-config
                                                               --spring.profiles.active=prod


The loaded application.yml is:
Greetings from: Z:\gs-spring-boot\complete\config\application.yml


11.

When started as follows:
java -jar target\gs-springboot.jar --spring.config.name=gs-spring-boot


Exception
Could not resolve placeholder 'location' in value "${location}"


12.

When started as follows:
Z:\gs-spring-boot\complete>java -jar target\gs-springboot.jar 
                                                              --spring.config.name=gs-spring-boot 
                                                              --spring.config.location=custom-config/


The loaded application.yml is:
Greetings from: Z:\gs-spring-boot\complete\custom-config\gs-spring-boot.yml