您的位置:首页 > 编程语言 > Java开发

51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

2017-11-07 17:02 736 查看
博客分类:
从零开始学Spring Bootspring bootspring boot实战spring
boot教程spring boot属性文件之多环境配置从零开始学Spring Boot 
      原本这个章节是要介绍《log4j多环境不同日志级别的控制的》但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也会提到属性文件基本的一些知识,然后慢慢在升华到多环境配置。

 

       相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷。我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁多的XML配置内容,替代它的是在
pom.xml
中引入模块化的
Starter
POMs
,其中各个模块都有自己的默认配置,所以如果不是特殊应用场景,就只需要在
application.properties
中完成一些属性配置就能开启各模块的应用。

 

       在之前的各篇文章中都有提及关于
application.properties
的使用,主要用来配置数据库连接、日志相关配置等。除了这些配置内容之外,本文将具体介绍一些在
application.properties
配置中的其他特性和使用方法。

 

自定义属性与加载

       这方面的知识我们在之前一篇文章

(25)Spring Boot使用自定义的properties【从零开始学Spring Boot】

 

中就已经有介绍过了,这里也当做温故下吧,当然重点还是之后的多环境配置文件。

       我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义:

在src/main/resources/application.properties:加入:

#自定义属性.
com.kfit.blog.name= Angel
com.kfit.blog.title=Spring Boot教程 

 

然后通过
@Value("${
属性名
}")
注解来加载对应的配置属性,具体如下:

(以下这种方式已经过时了,不推荐使用,但能正常运行的)。

@Component
public class BlogProperties {
   
    @Value("${com.kfit.blog.name}")
    private String
name;//博客作者
   
    @Value("${com.kfit.blog.title}")
    private String
title;//博客标题
 
    // 省略getter和setter
}
 

       通过单元测试来验证BlogProperties中的属性是否已经根据配置文件加载了。

引入单元测试依赖:

<!-- spring boot
单元测试. -->
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
       </dependency>
 

进行编码进行单元测试:

package com.kfit;
 
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.kfit.properties.BlogProperties;
/**
 *
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(App.class)
public class AppTest{
   
    @Autowired
    private BlogProperties
blogProperties;
 
    @Test
    public void testBlog()
throws Exception {
        System.out.println("AppTest.testBlog()="+blogProperties);
        Assert.assertEquals("Angel",blogProperties.getName());
        Assert.assertEquals("Spring Boot教程",
blogProperties.getTitle());
    }
}
 

       运行单元测试,完美的看到我们想要的结果了,但是我们刚刚提到了BlogProperties写法已经不推荐使用了,那么怎么写会比较perfect呢?看如下优雅的编码风格:

先引入spring boot提供的一个配置依赖:

<!--spring boot
配置处理器 -->      

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-configuration-processor</artifactId>
           <optional>true</optional>
        </dependency>
 
       在这里我们主要使用@ConfigurationProperties注解进行编码,

修改BlogProperties为:

 

/**
 * prefix="com.kfit.blog"

 *
 * 在application.properties配置的属性前缀,
 *
 * 在类中的属性就不用使用{@value}进行注入了。
 *
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
@ConfigurationProperties(prefix="com.kfit.blog")
public class BlogProperties {
   
    private String
name;//博客作者
   
    private String
title;//博客标题
 
    // 省略getter和setter
}
 

在启动类中加入:

@EnableConfigurationProperties({BlogProperties.class})

在运行单元测试还是能够正常进行的。

这种的好处就是只需要配置一个地方,其它地方就是正常定义类的属性即可了。

 

参数间的引用

       在
application.properties
中的各个参数之间也可以直接引用来使用,就像下面的设置:

 
com.kfit.blog.desc=${com.kfit.blog.name}正在写《${com.kfit.blog.title}》
 
 

com.kfit.blog.desc
参数引用了上文中定义的
name
title
属性,最后该属性的值就是
Angel
正在努力写《
Spring
Boot
教程》


 

使用随机数

       在一些情况下,有些参数我们需要希望它不是一个固定的值,比如密钥、服务端口等。Spring Boot的属性配置文件中可以通过
${random}
来产生int值、long值或者string字符串,来支持属性的随机值。

# 随机字符串
com.kfit.blog.value=${random.value} 

# 随机int
com.kfit.blog.number=${random.int} 

# 随机long
com.kfit.blog.bignumber=${random.long} 

# 10以内的随机数
com.kfit.blog.test1=${random.int(10)} 

# 10-20的随机数
com.kfit.blog.test2=${random.int[10,20]} 

 

 

通过命令行设置属性值

相信使用过一段时间Spring Boot的用户,一定知道这条命令:
java -jar xxx.jar --server.port=8888
,通过使用--server.port属性来设置xxx.jar应用的端口为8888。

在命令行运行时,连续的两个减号
--
就是对
application.properties
中的属性值进行赋值的标识。所以,
java
-jar xxx.jar --server.port=8888
命令,等价于我们在
application.properties
中添加属性
server.port=8888
,该设置在样例工程中可见,读者可通过删除该值或使用命令行来设置该值来验证。

通过命令行来修改属性值固然提供了不错的便利性,但是通过命令行就能更改应用运行的参数,那岂不是很不安全?是的,所以Spring Boot也贴心的提供了屏蔽命令行访问属性的设置,只需要这句设置就能屏蔽:
SpringApplication.setAddCommandLineProperties(false)


 在Eclipse中具体操作如下:

【右键工程】--【Run As】 --
【Run configurations】--找到【Arguments】--【Program arguments】--
添加【--server.port=8888】



 

 

 

多环境配置

       以上都不是重点,这才是重点,这才是重点,这才是重点,重要的事情说3遍。我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。

       对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,Spring Boot也不例外,或者说更加简单。

       在Spring Boot中多环境配置文件名需要满足
application-{profile}.properties
的格式,其中
{profile}
对应你的环境标识,比如:

   application-dev.properties:开发环境

   application-test.properties:测试环境

   application-prod.properties:生产环境

       至于哪个具体的配置文件会被加载,需要在
application.properties
文件中通过
spring.profiles.active
属性来设置,其值对应
{profile}
值。

如:
spring.profiles.active=test
就会加载
application-test.properties
配置文件内容

下面,以不同环境配置不同的服务端口为例,进行样例实验。

 

       针对各环境新建不同的配置文件
application-dev.properties
application-test.properties
application-prod.properties


       在这三个文件均都设置不同的
server.port
属性,如:dev环境设置为8080,test环境设置为9090,prod环境设置为80

       application.properties中设置
spring.profiles.active=dev
,就是说默认以dev环境设置

 

测试不同配置的加载:

       执行java -jar xxx.jar,可以观察到服务端口被设置为8080,也就是默认的开发环境(dev)

 

       执行java -jar xxx.jar --spring.profiles.active=test,可以观察到服务端口被设置为9090,也就是测试环境的配置(test)

 

       执行java -jar xxx.jar --spring.profiles.active=prod,可以观察到服务端口被设置为80,也就是生产环境的配置(prod)

 

按照上面的实验,可以如下总结多环境的配置思路:

   application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置

   application-{profile}.properties中配置各个环境不同的内容

通过命令行方式去激活不同环境的配置。

 

多环境高级应用

       在某些情况下,应用的某些业务逻辑可能需要有不同的实现。例如邮件服务,假设
EmailService
中包含的
send(String
email)
方法向指定地址发送电子邮件,但是我们仅仅希望在生产环境中才执行真正发送邮件的代码,而开发环境里则不发送以免向用户发送无意义的垃圾邮件。

       我们可以借助Spring的注解
@Profile
实现这样的功能,这样需要定义两个实现
EmailService
借口的类:

/**
 * 发送邮件接口.
 * @author Angel(QQ:412887952)
 * @version v.0.1
 */
public interface EmailService {
    /**发送邮件*/
    publicvoid send();
}

发送邮件的具体实现(dev-开发环境的代码):

@Service
@Profile("dev")
//开发环境的时候.
public class DevEmailServiceImpl
implements EmailService{
 
    @Override
    publicvoid send() {
       System.out.println("DevEmailServiceImpl.send().开发环境不执行邮件的发送.");
    }
}

 

 

发送邮件的具体实现(prod-生产环境的代码):

@Service
@Profile("prod")
//生产环境.
public class ProdEmailServiceImpl2
implements EmailService{
   
    @Override
    publicvoid send() {
       System.out.println("DevEmailServiceImpl.send().生产环境执行邮件的发送.");
       //具体的邮件发送代码.
       //mail.send();
    }
}
 

@Profile("dev")
表明只有Spring定义的Profile为
dev
时才会实例化
DevEmailService
这个类。那么如何设置Profile呢?

在配置文件中指定

 

application.properties
中加入:

spring.profiles.active=dev


 

通过命令行参数

java -jar app.jar --spring.profiles.active=dev


  【Spring Boot 系列博客】

视频&交流平台:

à Spring Boot网易云课堂视频
http://study.163.com/course/introduction.htm?courseId=1004329008
à Spring Boot交流平台
http://412887952-qq-com.iteye.com/blog/2321532
 

网易云课堂视频最新更新

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

第十二章 Spring Boot 知识点2

1、spring boot 服务配置和部署

2、Spring Boot 定制URL匹配规则

 
 
历史章节

 

第一章 快速开始

1、Spring Boot之Hello World

2、Spring Boot之Hello World访问404

 

第二章 Spring Boot之JSON

1、spring boot返回json数据

2、Spring Boot完美使用FastJson解析JSON数据

 

第三章 Spring Boot热部署

1、Spring Boot热部署(springloader)

2、springboot + devtools(热部署)

 

第四章 Spring Boot数据库

1、Spring Boot JPA/Hibernate/Spring Data概念

2、Spring Boot JPA-Hibernate

3、Spring Boot Spring Data JPA介绍

4、Spring Boot JdbcTemplate

5、Spring Boot集成MyBatis

 

第五章 web开发

1、全局异常捕捉

2、配置server信息

3、spring boot使用thymeleaf

4、Spring Boot 使用freemarker

5、Spring Boot添加JSP支持

 

第六章 定时任务

1、Spring Boot定时任务

2、Spring Boot 定时任务升级篇(动态修改cron参数)

3、Spring Boot 定时任务升级篇(动态添加修改删除定时任务)

4、Spring Boot 定时任务升级篇(集群/分布式下的定时任务说明)

5、Spring Boot Quartz介绍

6、Spring Boot Quartz在Java Project中使用

7、Spring Boot 集成Quartz普通使用

8、Spring Boot 集成Quartz升级版

9、Spring Boot 集成Quartz二次升级版

10、Spring Boot 集成Quartz-Job如何自动注入Spring容器托管的对象

 

第七章 Spring Boot MyBatis升级篇

1、Spring Boot MyBatis升级篇-注解

2、Spring Boot MyBatis升级篇-注解-自增ID

3、Spring Boot MyBatis升级篇-注解-增删改查

4、Spring Boot MyBatis升级篇-注解-分页查询

5、Spring Boot MyBatis升级篇-注解-分页PageHelper不生效

6、Spring Boot MyBatis升级篇-注解- mybatic insert异常:BindingException: Parameter 'name' not found

7、Spring Boot MyBatis升级篇-注解- #和$符号特别篇

8、Spring Boot MyBatis升级篇-注解-@Result

9、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案一:<script>

10、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案二:@Provider

11、Spring Boot MyBatis升级篇-注解-动态SQL-参数问题

12、Spring Boot MyBatis升级篇-注解-特别篇:@MapperScan和@Mapper

13、Spring Boot MyBatis升级篇-XML

14、Spring Boot MyBatis升级篇-XML-自增ID

15、Spring Boot MyBatis升级篇-XML-增删改查

16、Spring Boot MyBatis升级篇-XML-分页查询

17、Spring Boot MyBatis升级篇-XML-分页PageHelper不生效

18、Spring Boot MyBatis升级篇-XML-动态SQL(if test)

19、Spring Boot MyBatis升级篇-XML-注解-初尝试

20、Spring Boot MyBatis升级篇- pagehelper替换为pagehelper-spring-boot-starter

 

第八章 Spring Boot 知识点1

1、Spring Boot 拦截器HandlerInterceptor

2、Spring Boot启动加载数据CommandLineRunner

3、Spring Boot环境变量读取和属性对象的绑定

4、Spring Boot使用自定义的properties

5、Spring Boot使用自定义的properties

6、Spring Boot使用@SpringBootApplication

7、Spring Boot 监控和管理生产环境

 

第十章 Spring Boot 打包部署

1、Spring Boot打包部署((提供Linux的sh文件))

 

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

更多查看博客: http://412887952-qq-com.iteye.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐