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

  1. pom.xml file and add the following dependency:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>                                        <optional>true</optional>
        </dependency>
  1. Modify pom.xml file
 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--开启热部署-->
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

After modifying pom.xml file, idea will pop up a file lets you automatically import the package, 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.

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

Select the image aboveBuild Project automatical

  1. Press the key combination Shift+ALT+Ctrl+/to select Registrycan see the following interface

Select the image abovecomplier.automake.allow.when.app.running

Thus, the revised our code, without having to re-build also require a restart.

Two, Spring Boot reads the configuration file

  1. Modify our configuration fileapplication.properties
server.port=8888

<!--网站配置-->
website.name=Loading
website.domin=www.loading.ink
website.title=我的博客网站
website.description=分享我的生活和技术
  1. New profile classWebSiteConfig
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 configuration file attributes

note:

Annotations @Configurationfor defining configuration class

Notes @PropertySource(value = "classpath:application.properties")on behalf of the path you want to read the configuration file when the configuration file is application.propertiestime, this annotation can be omitted

  1. NewWebSiteController
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();
    }
}
  1. Test Run

  1. Note that the problems encountered reading the configuration file of Chinese garbled if the first run. The main reason is probably because the format of the configuration file format is not utf-8's. In this case the idea may be provided.

如上图所示更改为uft-8,注意后面的一定要勾选上。

通过上面配置后如果还不行,可以将配置文件删除后重新建一个,问题就可以解决!

三、Spring Boot Profile

    Spring Boot 使用一个全局的配置文件 application.properties ,Spring Boot 的全局配置文件的作用是对一些默认配置的配置值进行修改。

    在日常开发中,我们常常会遇到一个问题。就是在不同的环境使用不同的配置。比如生产、开发、测试三个不同的环境,我们的配置肯定不一样。这时,我们就要用到Profile。

    Profile 是 Spring 用来针对不同的环境对不同的配置提供支持的,全局 Profile 配置使用 application-{profile}.properties(如 application-dev.properties)。通过在 application.properties 中设置 spring.profiles.active = dev 来指定活动的 Profile

  1. 依次再目录下面新建三个配置文件,application-dev.propertiesapplication-test.propertiesapplication-prod.properties。它们分别代表开发环境、测试环境、生产环境的配置文件。
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=分享我的技术与生活
  1. 接下来修改application.properties:表示,将采用application-dev.properties这个配置文件。
spring.profiles.active=dev

测试运行我们可以看到会启动开发环境配置文件的端口8887

注意:配置文件会优先获取Profile中的配置,如果Profile中没有的配置项, 那么会直接取application.properties中的配置

示例代码

Guess you like

Origin www.cnblogs.com/hunternet/p/11588734.html