Spring Boot学习——Spring Boot配置文件application

        Spring Boot配置文件有两种格式: application.properties 和 application.yml。两种配置文件只需要使用一个。

        这两种配置文件的语法有些区别,如下

                1. application.properties

                        server.port = 8080         -- tomcat 端口

                        server.context-path = /webName    -- URL路径

                2. application.yml

                        server:

                                port: 8080         -- tomcat 端口,注意冒号后面有空格

                                context-path: /webName    -- URL路径,注意冒号后面有空格

        一、Java类中使用配置

                1. 方法一

@value("${server.port}")
private String port;

 

                2. 方法二

@Compoent
@ConfigurationProperties(prefix="server")
public class ServerProperties{
      private String port;
      private String context-path;
      
      // set/get方法      
}

    注意:使用注解 @Compoent是为了方便在其他类中使用@Autowired引用该类

        二、分环境使用配置文件

                再创建两个配置文件 application-dev.yml(测试环境配置文件) 和 application-prod.yml(正式环境配置文件)

 

                在 application.yml 中配置如下:

spring:
    profiles:
        active: dev

                注: 上面的配置是使用配置文件application-dev.yml,改成 active:prod即可使用配置文件application-prod.yml

        三、java命令启动使用配置

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

                注:上面的配置是使用配置文件application-dev.yml,改成 --spring.profiles.active=prod即可使用配置文件application-prod.yml

posted @ 2017-07-25 23:03  aston  阅读(424)  评论(0编辑  收藏  举报