3、项目属性配置

  1. 在application.properties当中配置端口号
    1. server.port=8081
  2. 在application.properties当中配置项目路径
    1. server.context-path=/girl
  3. 启动项目
    1. 访问 localhost:8081/girl/hello
  4. 也可以将 pplication.properties也可以写成
    1. application.yml文件



  1. 通过注解,将application.yml当中的变量在.java文件当中取出使用
    1. @Value("${cupSize}")
    2. 在yml文件当中添加cupSize:xxx 属性
  2. 在配置当中使用当前配置
    1. cupSize: B
      age: 18
      content: "cupSize: ${cupSize},age:${age}"
  3. 在.yml文件当中使用封装
    1. server:
        port: 8080
      girl:
        cupSize: B
        age: 18
      
    2. 然后创建针对封装的类使用配置文件当中的数据
      1. package com.girl;
        
        import org.springframework.boot.context.properties.ConfigurationProperties;
        import org.springframework.stereotype.Component;
        
        /**
         * Created by sunnyangzs on 2018/3/26.
         */
        @Component
        @ConfigurationProperties(prefix = "girl")
        public class GirlProperties {
        
            private  String cupSize;
            private Integer age;
        
            public String getCupSize() {
                return cupSize;
            }
        
            public void setCupSize(String cupSize) {
                this.cupSize = cupSize;
            }
        
            public Integer getAge() {
                return age;
            }
        
            public void setAge(Integer age) {
                this.age = age;
            }
        }
        
    3. 然后在主文件当中调用
      1. package com.girl;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.bind.annotation.RestController;
        
        /**
         * Created by sunnyangzs on 2018/3/25.
         */
        @RestController
        public class HelloController {
        
        
            @Autowired
            private GirlProperties girlProperties;
        
            @RequestMapping(value = "/hello",method = RequestMethod.GET)
            public String say(){
                return girlProperties.getCupSize();
            }
        
        }
        
  4. 针对两套不同的配置文件,开发环境和生产环境
    1. application-dev.yml
    2. application-prod-yml
    3. 需要在application.yml文档当中配置使用哪一个配置文件
      1. spring:
          profiles:
            active: dev
  5. 生产环境和开发环境同时使用
    1. 使用两套系统,一套打包成jar包执行,一套可以在IDE当中执行,分别设置不同的端口就可以了,然后在命令式使用jar执行的时候,使用如下的命令
      1. java -jar girl-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod




posted @ 2018-03-26 08:22  sunnyangzs  阅读(278)  评论(0编辑  收藏  举报