springboot-springboot interview questions and answers series 4

1. The purpose of this post

I would show some questions and answers of springboot interviews.

2. Environments

  • springboot 1.x and 2.x

3. Springboot Interview Questions and Answers Series 4

3.1 How to define and use different profiles with Spring Boot?

Sometimes, we want to use different profiles with different scenarios, for example we want to use test or dev profile when developing, and use production profile when deployed in production environment.

3.2 Control your profile in your application-[profileName].properties

You can define different profile files by the format application-[profileName].properties, for example, you can define the following properties files for default,dev and production environments:

  • application.properties
    • Used by the default profile
  • application-dev.properties
    • Used by dev profile
  • application-prod.properties
    • Used by production profile

Let’s add some properties in them:

application.properties:

info.name=Mike default
info.desc=Good boy default

application-dev.properties:

info.name=Mike dev

application-prod.properties:

info.name=Mike prod

You can see that there is a common property named info.name in all the files, but we ignored the info.desc in dev and prod profile properties, Let’s see what would happen.

3.3 Read and print the properties

You can follow this article to write a CommandLineRunner and print the properties.

The code:

package com.test.sb1jt.commands;

import com.test.sb1jt.config.InfoConfig;
import com.test.sb1jt.config.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * Created on 2019/5/22.
 */
@Component
public class MyCommand implements CommandLineRunner {
    private static Logger logger = LoggerFactory.getLogger(MyCommand.class);


    @Value("${info.name}") // use @Value to inject the properties
    private String name;

    @Value("${info.desc}") // use @Value to inject the properties
    private String desc;

    @Override
    public void run(String... strings) throws Exception {
        logger.info("MyCommand run");

        logger.info("name is {},desc is {}",name,desc);

    }
}

3.3 Demo how to switch between profiles

Before switching profiles, let’s run the default profile at first:

2019-05-24 22:44:46.885  INFO 51586 --- [  restartedMain] com.test.sb1jt.commands.MyCommand        : name is Mike default,desc is Good boy default

You can see that, springboot read properties from application.properties by default.

If you want to switch to dev, just do as follows:

Add spring.profiles.active property to application.properties:

spring.profiles.active=dev

Then rerun the app:

2019-05-24 22:46:33.885  INFO 51594 --- [  restartedMain] com.test.sb1jt.commands.MyCommand        : name is Mike dev,desc is Good boy default

You can see that Springboot will read the properties of the active profile first. If not defined, the default configuration will be used.

Switch to the prod profile is the same as the dev switching process:

Add spring.profiles.active property to application.properties:

spring.profiles.active=prod

Then rerun the app:

2019-05-24 22:46:33.885  INFO 51594 --- [  restartedMain] com.test.sb1jt.commands.MyCommand        : name is Mike prod,desc is Good boy default

4. Conclusion

Springboot provide the spring.profiles.active in your application.properties to control the active profile, you can read the offical documents about profiles to get more information about this topic.