Categories
Uncategorized

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:

        
            org.springframework.boot
            spring-boot-devtools                                        true
        

    Modify pom.xml file

 
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                    true
                
            
        
    

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 complier.automake.allow.when.app.running

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 application.properties

server.port=8888


website.name=Loading
website.domin=www.loading.ink
website.title=我的博客网站
website.description=分享我的生活和技术

    New profile class WebSiteConfig

package spring.boot.web.config;

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

import java.text.MessageFormat;

@Configuration
//@ConfigurationProperties(prefix = "website")
//要读取的配置文件地址
@PropertySource(value = "classpath:application.properties")
public class WebSiteConfig {
    @Value("${website.title}")
    private String title;
    @Value("${website.domain}")
    private String domain;
    @Value("${website.description}")
    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;
    }

    @Override
    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

note:

Configuration class for defining annotation @Configuration

Notes @PropertySource (value = “classpath: application.properties”) on behalf of the path you want to read the configuration file when the configuration file is application.properties 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;

@RequestMapping("/website")
@RestController
public class WebSiteController {

    //通过@Autowired注解注入bean
    @Autowired
    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 application.properties. 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 application-dev.properties). By providing the application.properties spring.profiles.active = dev specified in Profile activities.

    Create three new configuration files under the directory, application-dev.properties, application-test.properties, and application-prod.properties. They represent the configuration files of the development environment, the test environment, and the production environment.

server.port=8887

website.title=我的博客网站--Dev
website.domain=www.loading.ink
website.description=分享我的技术与生活
server.port=8886

website.title=我的博客网站--test
website.domain=www.loading.ink
website.description=分享我的技术与生活
server.port=8885

website.title=我的博客网站--prod
website.domain=www.loading.ink
website.description=分享我的技术与生活

    Next, modify application.properties: means that application-dev.properties will be used as the configuration file.

spring.profiles.active=dev

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 application.properties configuration

Sample Code

Leave a Reply