Spring Boot系列(二):Spring Boot的配置文件

Spring Boot 支持多种外部配置方式

一:应用配置文件;

1:默认配置方式:application.properties

server:port: 8080
server.context-path=/student

此种配置方式必须要写全,不够简化,server不能省略,

2:j简化的配置方式:yml模式;

server:
  port: 8080
  address: 192.168.1.100

此配置相对简化一些server可以不写,,建议开发使用此[配置文件;具体可以参考这里

3:如果在配置文件中有几个属性怎么解决!

比如:学生的多个属性;

server:
  port: 8080
  address: 192.168.1.100
sName: 鬼塚
age: 12
gender: 
这样写在controller中怎么处理!

创建controller处理:

处理方式:使用@Value自动注入配置文件中的属性;配置文件不需要指定变量类型一切只需要在controller中写即可

package com.gmm.controller;

        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 john on 2017-04-29.
 */
@RestController
public class StudentController {

    @Value("${sName}")  //@value次注解是讲配置文件中的配置信息自动注入到变量里面;
    private String sName;

    @Value("${age}")
    private Integer age;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String helllo(){
        return sName+age;

    }
}
我们发现这样的写法比较繁琐,如果属性过多就不好写了所以下面请看第二种方式也是推荐大家使用的方式;

1:首先看配置文件;修改后的配置文件相当于给属性添加了一个student的前缀,他有三个属性;

server:
  port: 8080
student:
         sName: 鬼塚
          age: 12
         gender: 
2:controller处理这些配置文件

第一步:创建一个StudentProperties文件;这个类包含配置文件的属性;

package com.gmm.model;

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

/**
 * Created by john on 2017-04-29.
 */
@Component
@ConfigurationProperties(prefix = "student")//获取前缀为stduent配置信息;
public class StudentProperties {

    private String sName;
    private Integer age;
    private String gender;

    public String getsName() {
        return sName;
    }
    public void setsName(String sName) {
        this.sName = sName;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
}

注意:@ConfigurationProperties这个注解他有两个个属性

  • locations:他制定了配置文件的属性;
  • prefix:指定配置文件中键的名称的前缀

@Component这个注解必须要,不然在controller会报错无法注入

第二步:怎么获取类的属性创建controller;如下:

package com.gmm.controller;

import com.gmm.model.StudentProperties;
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;

/**
 * Created by john on 2017-04-29.
 */
@RestController
public class StudentController {

    @Autowired
    private StudentProperties studentProperties;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String helllo(){
        return studentProperties.getGender();

    }
}

以上我们可以看到在StudentProperties我们使用了@Compoment注解,所以在controller直接使用@Autowired注解进行自动注入前缀为stduent的属性,

在项目中推荐使用这种方式来获取配置文件的参数;

注:在项目开发中往往环境不一样那么在不同的环境会有的不同的配置

解决:写两个配置文件项目中在不同的环境中引入对应的配置文件即可!

.yml方式:

开发环境:application-dev.yml

student:
  sName: 鬼塚
  age: 12
  gender: 

生产环境:application-prod.yml

ent:
  sName: 冬月
  age: 13
  gender: 
核心配置文件引入不同环境application.yml

spring:
  profiles:
    active: dev
server:
  port: 8080

注意?:核心配置文件只需要引入不同环境的配置完文件即可;

.Properties方式

开发环境:application-dev.properties

student.sName= 鬼塚
student.age= 12
student.gender= 

生产环境:application-prod.properties

student.sName=冬月
student.age=13
student.gender=

核心配置文件:application.properties

#spring配置文件环境
spring.profiles.active= dev

#端口
server.port= 8080
spring.http.encoding.force=true
注意:两种方式的比较

  • .properties这个格式要写等于号,.ym方式用:号
  • 两者的编码格式不一样,使用时防止中文乱码
















































































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值