码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot 配置详解

时间:2018-12-03 17:51:27      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:.property   inner   return   final   三方   director   email   ppi   BYD   

为了让Spring Boot更好的生成配置元数据文件,我们需要添加如下依赖(不然没有提示就苦逼了),该依赖只会在编译时调用,所以不用担心会对生产造成影响…

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

自定义属性配置

在 application.properties 写入如下配置内容

winner.name = winner_0715
winner.address = beijing

PropertiesDemo1.java文件,用来映射我们在application.properties中的内容,这样一来我们就可以通过操作对象的方式来获得配置文件中的配置项了

package com.winner.service;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 对应默认的application.properties文件
 * @author winner_0715
 * @description:
 * @date 2018/12/3
 */
@Component
@ConfigurationProperties(prefix = "winner")
public class PropertiesDemo1 {

    /**
     * 对应application.properties中去掉前缀之后的配置
     */
    private String name;

    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("MyProperties1{");
        sb.append("name=‘").append(name).append(‘\‘‘);
        sb.append(", address=‘").append(address).append(‘\‘‘);
        sb.append(‘}‘);
        return sb.toString();
    }
}

接下来就是定义我们的 PropertiesController用来注入PropertiesDemo1测试我们编写的代码,

package com.winner.web;

import com.winner.service.PropertiesDemo1;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;

/**
 * @author winner_0715
 * @description:
 * @date 2018/12/3
 */
@RestController
public class PropertiesController {

    @Autowired
    private PropertiesDemo1 propertiesDemo1;

    @RequestMapping(value = "/properties/get/1", method = RequestMethod.GET)
    public PropertiesDemo1 getPropertiesDemo1() {
        return propertiesDemo1;
    }
}

自定义文件配置

定义一个名为demo.properties的资源文件,自定义配置文件的命名不强制 application 开头

winner.name=winner_0715
winner.address=beijing
winner.email=winner_0715@163.com

其次定义PropertiesDemo2.java文件,用来映射我们在demo.properties中的内容。

package com.winner.service;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @author winner_0715
 * @description:
 * @date 2018/12/3
 */
@Component
@PropertySource("classpath:demo.properties")
@ConfigurationProperties(prefix = "winner")
public class PropertiesDemo2 {
    private String name;
    private String address;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("PropertiesDemo2{");
        sb.append("name=‘").append(name).append(‘\‘‘);
        sb.append(", address=‘").append(address).append(‘\‘‘);
        sb.append(", email=‘").append(email).append(‘\‘‘);
        sb.append(‘}‘);
        return sb.toString();
    }
}

接下来在 PropertiesController用来注入 PropertiesDemo2 测试我们编写的代码

package com.winner.web;

import com.winner.service.PropertiesDemo1;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;

/**
 * @author winner_0715
 * @description:
 * @date 2018/12/3
 */
@RestController
public class PropertiesController {

    @Autowired
    private PropertiesDemo2 propertiesDemo2;

    @RequestMapping(value = "/properties/get/2", method = RequestMethod.GET)
    public PropertiesDemo2 getPropertiesDemo2() {
        return propertiesDemo2;
    }

}

多环境化配置

在真实的应用中,常常会有多个环境(如:开发,测试,生产等),不同的环境相关的配置不同,如数据库连接,第三方接口url,这个时候就需要用到spring.profile.active的强大功能了,它的格式为 application-{profile}.properties,这里的 application 为前缀不能改,{profile}是我们自己定义的。

在pom.xml中增加不同环境打包的配置:

<!-- 不同环境查找不同配置文件 -->
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>beta</id>
        <properties>
            <profiles.active>beta</profiles.active>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <profiles.active>prod</profiles.active>
            <maven.test.skip>true</maven.test.skip>
            <scope.jar>provided</scope.jar>
        </properties>
    </profile>
</profiles>
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>application-dev.properties</exclude>
                <exclude>application-beta.properties</exclude>
                <exclude>application-prod.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>application-${profiles.active}.properties</include>
                <include>application.properties</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

使用占位符,在打包时替换,首先在配置文件中增加:

spring.profiles.active=@profiles.active@

执行打包命令:

 mvn package -Ptest  

 

SpringBoot 配置详解

标签:.property   inner   return   final   三方   director   email   ppi   BYD   

原文地址:https://www.cnblogs.com/winner-0715/p/10059770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!