Spring Boot (b) Profiles

Post navigation -readme

First, configure the Spring Boot hot deployment

Because of the psychological development of technology people always want to be lazy, if we do not want to modify the code every time, you must restart the server, and re-run the code. You can configure the look hot deployment. With it later, modify the code only need to re-build it, you can see the effect, you do not need to restart the server.

1. Configure hot deployment

    pom.xml file and add the following dependency:

            spring-boot-devtools                                        true

    Modify pom.xml file


After modifying pom.xml file, idea will pop up to let you automatically import a package file, click Import Changes. Jar package will automatically be downloaded to rely library.

So, to achieve a hot deployment Spring Boot at this time to modify our code, simply re Build click on it.

2. Configure automatic build

Of course, if you’re more lazy, then practicing build not want to do, you can also be configured to automatically Build (Build automatically supports only Spring Boot project) in the Idea.

    Open Idea-> File-> Settings … you can see the following interface

Select the image above Build Project automatical

    Press the key combination Shift + ALT + Ctrl + / Registry can see the following interface selection

Select the image above

In this way, there is no need to re-build or restart after modifying our code.

Two, Spring Boot reads the configuration file

    Modify our configuration file


    New profile class WebSiteConfig

package spring.boot.web.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.text.MessageFormat;

//@ConfigurationProperties(prefix = "website")
@PropertySource(value = "")
public class WebSiteConfig {
    private String title;
    private String domain;
    private String description;

    public String getTitle() {
        return title;

    public void setTitle(String title) {
        this.title = title;

    public String getDomain() {
        return domain;

    public void setDomain(String domain) {
        this.domain = domain;

    public String getDescription() {
        return description;

    public void setDescription(String description) {
        this.description = description;

    public String toString() {
        return MessageFormat.format("Title:{0} Domin:{1} Description:{2}", title, domain, description);

Reads the configuration file with either of two ways

1. @ ConfigurationProperties (prefix = “website”) for binding properties, wherein the prefix represents a prefix bound property. If the configuration file in the same configuration and attribute names can be used in this way

2. @ Value ( “$ {website.title}”) binding profile attributes


Configuration class for defining annotation @Configuration

Notes @PropertySource (value = “classpath:”) on behalf of the path you want to read the configuration file when the configuration file is time, this annotation can be omitted

    New WebSiteController

package spring.boot.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import spring.boot.web.config.WebSiteConfig;

public class WebSiteController {

    private WebSiteConfig webSiteConfig;

    @RequestMapping(method = RequestMethod.GET)
    public String get() {
        return webSiteConfig.toString();

    Test Run

    Note that if you encounter the problem of reading the Chinese garbled in the configuration file for the first time you run it. The main reason for this is probably because the format of the configuration file is not utf-8. This can be set in the idea.

Change to uft-8 as shown above. Be sure to check the following.

If it doesn’t work through the above configuration, you can delete the configuration file and recreate it, and the problem can be solved!

Three, Spring Boot Profile

Spring Boot uses a global configuration file The purpose of Spring Boot’s global configuration file is to modify some of the default configuration values.

In everyday development, we often encounter a problem. is to use different configurations in different environments. For example, production, development and testing three different environments, our configuration is certainly different. At this point, we need to use Profile.

Spring Profile is used to provide support for different environments for different configurations, the configuration using the global Profile application- {profile} .properties (eg By providing the = dev specified in Profile activities.

    Create three new configuration files under the directory,,, and They represent the configuration files of the development environment, the test environment, and the production environment.





    Next, modify means that will be used as the configuration file.

Test run We can see port 8887 that will start the development environment configuration file

Note: The profile will give priority to get Profile configuration, if not in the Profile configuration items, it will directly take configuration

Sample Code

Leave a Reply